I have a class that inherits the private members of a class called LinkedList. They are both template classes. I am getting these compiler errors but I am not sure what I am missing:
1 2 3 4
Stack.h: In constructor âstack<T>::stack()â:
Stack.h:13: error: missing template arguments before â(â token
Stack.h: In copy constructor âstack<T>::stack(const stack<T>&)â:
Stack.h:16: error: missing template arguments before â(â token
Here is the class:
1 2 3 4 5 6 7 8 9
template <class T>
class stack: public LinkedList<T>
{
public:
//default constructor
stack() {LinkedList();}
//copy constructor
stack(const stack<T> & rhs) {LinkedList(rhs);}
Within the body of stack's constructor, the (pointless) declaration of LinkedList is missing a template parameter. This is the same for the LinkedList declaration within stack's copy constructor. It should be: LinkedList< T > Something;
but It was giving me errors as well. I'm not sure what should come after the LinkedList< T >. I thought in my stack constructor, since it is using the same member variables as LinkedList, I could just call the LinkedList destructor, but obviously that hasn't been working.
I thought in my stack constructor, since it is using the same member variables as LinkedList, I could just call the LinkedList destructor, but obviously that hasn't been working.
Why would you call the destructor in a constructor? I assume you got mixed up?
Did you mean something like this:
1 2 3 4 5 6 7 8 9 10
template <class T>
class stack: public LinkedList<T>
{
public:
//default constructor
stack() : LinkedList< T >( ) { }
//copy constructor
stack(const stack<T> & rhs) : LinkedList< T >( rhs ) { }
};
Yes I'm sorry I mean't to say constructor. Buy anyway, I've changed it to what you wrote and it's gotten rid of those errors, but now I have some undefined reference errors in my gtests in case you want to keep helping.
Here is just on of the errors, considering they are almost identical, just varying on which function it has a problem with:
test_stack.cpp:17: undefined reference to `stack<unsignedint>::stack()'
To my untrained eye it's apparently not recognizing my stack class even though I believe I have all of the proper #include's that I need.
1 2 3 4 5 6 7 8
TEST (StackTest, DefaultConstructor)
{
// Create stack with default constructor
stack<unsignedint> a;
// Test whether size is 0 and isEmpty is true
ASSERT_EQ(0, a.size()) << "\n** Bad DefaultConstructor\n";
ASSERT_NOT_NULL(a.isEmpty()) << "\n** isEmpty should be true\n";
}
When you separate the definition from the declaration, the linker won't be able the locate the definition it needs to successfully create a class template. Because headers aren't compiled, they don't have an object file. The linker uses object files (which contain the compiled code) to link definitions to declarations. You can solve this in one of 2 ways:
1) #includeing the header where the definitions are at the bottom of the header where the declarations are
2) Place the definitions beneath the class