Missing Template argument

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);}


What exactly am I missing?
closed account (zb0S216C)
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;

Wazzak
Last edited on
I've tried

LinkedList<T>::LinkedList();

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.
closed account (zb0S216C)
RonTheMoron wrote:
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 ) { }
};


Wazzak
Last edited on
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<unsigned int>::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<unsigned int> 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";
}
closed account (zb0S216C)
Have you defined your templates within a source file?

Wazzak
Yes, I have a stack template and a LinkedList template in two separate header files
closed account (zb0S216C)
Good. Now, are the template definitions within the same header file (LinkedList in one, while Stack is in the other)?

Wazzak
Last edited on
no, I have them in separate .hpp files and have #include "linkedList.h" and "stack.h" at the top
closed account (zb0S216C)
I suspected that much. You have to have the definition within the same file as the declaration. For example:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
// Declarations...
template< typename T >
class List
{
    public:
        List( );
};

// Definitions....
template< typename T >
List< T >::List( )
{
    //...
}


Wazzak
What in this particular instance is making it so I can't have them in separate files like I've done before?
closed account (zb0S216C)
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

Wazzak
Last edited on
Topic archived. No new replies allowed.