I am writing a templated stack class for a programming class, and I cannot for the life of me get it to compile.
My IDE is Visual Studio Professional 2013.
I previously had issues about visual studio thinking the constructor needed a return type defined, but that seems to have fixed itself.
Now Visual Studio is presenting me with 4 errors:
1>main.obj : error LNK2019: unresolved external symbol "public: __thiscall StackOfAnything<int>::StackOfAnything<int>(void)" (??0?$StackOfAnything@H@@QAE@XZ) referenced in function _wmain
1>main.obj : error LNK2019: unresolved external symbol "public: __thiscall StackOfAnything<int>::~StackOfAnything<int>(void)" (??1?$StackOfAnything@H@@QAE@XZ) referenced in function _wmain
1>main.obj : error LNK2019: unresolved external symbol "public: __thiscall StackOfAnything<char>::StackOfAnything<char>(void)" (??0?$StackOfAnything@D@@QAE@XZ) referenced in function _wmain
1>main.obj : error LNK2019: unresolved external symbol "public: __thiscall StackOfAnything<char>::~StackOfAnything<char>(void)" (??1?$StackOfAnything@D@@QAE@XZ) referenced in function _wmain
1>c:\users\kurt slagle\documents\visual studio 2013\Projects\StackOfAnything\Debug\StackOfAnything.exe : fatal error LNK1120: 4 unresolved externals
My code is below
for StackOfAnything.h
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
|
#ifndef STACK_OF_ANYTHING_H
#define STACK_OF_ANYTHING_H
#include "BoxOfAnything.h"
#include <stdexcept>
template<class T>
class StackOfAnything
{
public:
StackOfAnything();
~StackOfAnything();
bool isEmpty() const;
int size() const;
void push(T value);
T peek() const throw(std::runtime_error);
T pop() throw(std::runtime_error);
private:
BoxOfAnything<T>* m_top;
int m_size;
};
#endif
|
for StackOfAnything.cpp
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67
|
#include "StackOfAnything.h"
#include <iostream>
#include <stdexcept>
template<class T>
StackOfAnything<T>::StackOfAnyting()
{
m_top = nullptr;
m_size = 0;
}
template<class T>
StackOfAnything<T>::~StackOfAnything()
{
while (!isEmpty())
pop();
}
template<class T>
bool StackOfAnything<T>::isEmpty() const
{
if (m_size > 0)
return false;
else
return true;
}
template<class T>
int StackOfAnything<T>::size() const
{
return m_size;
}
template<class T>
void StackOfAnything<T>::push(T value)
{
BoxOfAnything<T>* temp = m_top;
m_top = new BoxOfAnything < T > ;
m_top->setValue(value);
m_top->setPrevious(temp);
m_size++;
}
template<class T>
T StackOfAnything<T>::peek() const throw(std::runtime_error)
{
if (m_size <= 0)
throw (std::runtime_error("Peek attempted on empty stack"));
else
return m_top->getValue();
}
template<class T>
T StackOfAnything<T>::pop() throw(std::runtime_error)
{
if (!isEmpty())
{
T tempval = m_top->getValue();
BoxOfAnything<T>* temp = m_top->getPrevious();
delete m_top;
m_top = temp;
m_size--;
return tempval;
}
else
throw (std::runtime_error("Pop attempted on empty stack"));
}
|
for BoxOfAnything.h
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
|
#ifndef BOX_OF_ANYTHING_H
#define BOX_OF_ANYTHING_H
using namespace std;
template<class T>
class BoxOfAnything
{
public:
BoxOfAnything(T value);
BoxOfAnything<T>* getPrevious();
T getValue();
void setValue(T value);
void setPrevious(BoxOfAnything<T>* next);
private:
BoxOfAnything<T>* m_previous;
T m_value;
};
#endif
|
for BoxOfAnything.cpp
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
|
#include "BoxOfAnything.h"
template<class T>
BoxOfAnything<T>::BoxOfAnything(T value)
{
m_value = value;
m_previous = nullptr;
}
template<class T>
BoxOfAnything<T>* BoxOfAnything<T>::getPrevious()
{
return m_previous;
}
template<class T>
T BoxOfAnything<T>::getValue()
{
return m_value;
}
template<class T>
void BoxOfAnything<T>::setValue(T value)
{
m_value = value;
}
template<class T>
void BoxOfAnything<T>::setPrevious(BoxOfAnything<T>* next)
{
m_previous = next;
}
|
I am NOT asking to get advice for improving my code. My class is very strict, and I am not allowed to use any other methods than the ones that I have already used (including other libraries, other macros, etc).
I have no idea why this is not compiling. Any help would be greatly appreciated.
I was able to write a regular stack project just fine, but this one, for some reason, just will not work. I love Visual Studio, but I hate it as well.
Thank you from a programmer who has been banging his head on his keyboard all morning.