Reputation: 189
I need to turn a linked list class for ints that I wrote into a class template. I'm having issues with the constructor and destructor for a struct nested in the List Class, called node.
Layout:
template <typename T>
class List
{
public:
//Stuff that's not important to this question
private:
struct Node
{
Node(T value); // constructor
~Node(); // destructor
Node *next; // pointer to the next Node
T data; // the actual data in the node
static int nodes_alive; // count of nodes still allocated
};
};
Implementation:
template <typename T>
typename List<T>::Node::Node(T value)
{
data = value;
next = 0;
}
template <typename T>
typename List<T>::Node::~Node()
{
--nodes_alive;
}
Errors:
Expected ';' at end of declaration
typename List::Node::Node(T value)
Expected an identifier or template ID after '::'
typename List::Node::~Node()
Expected the class name after '~' to name a destructor
typename List::Node::~Node()
Not really sure what's going on here. My implementation is in a separate file included at the bottom of the header file. Any help would be greatly appreciated.
Upvotes: 1
Views: 278
Reputation: 5729
It's simple: Get rid of the typename
keyword. Since you're writing a constructor/destructor and there is no return type, it is not needed.
template <typename T>
List<T>::Node::Node(T value)
{
data = value;
next = 0;
}
template <typename T>
List<T>::Node::~Node()
{
--nodes_alive;
}
Upvotes: 3