kdubs kdubs - 15 days ago 5
C++ Question

C++ LinkList and Node Template Linking Errors

I am trying to create a node template to use with my linked list template but am getting an error that my constructors in Node.h are not defined. I have a Node.h file and a Node.tem file that I created in Visual Studio. The Node.h file looks like this:

#ifndef NODE_H
#define NODE_H

#include <cstdlib>

template <class Type>
class Node
{
public:
Node();
Node(Type indata);

Type data;
Node<Type>* next;
Node<Type>* prev;

};
#include "Node.tem"
#endif


and my Node.tem file looks like this:

template <class Type>
Node<Type>::Node()
{
next = nullptr;
}

template <class Type>
Node<Type>::Node(Type indata)
{
data = indata;
next = nullptr;
}


After some debugging, it looks like the problem occurs in my alloc function in the Linked List template on this bit of code:

template <class Type>
Node<Type>* LinkList<Type>::alloc(Type indata)
{
Node<Type>* dynamicNode = new Node(indata); //error occurs here
return dynamicNode;
}


The errors I get are:

'Node': class has no constructors
and
'Node': use of class template requires template argument list


My
main()
code is rather large as this is just a small piece of a big project, but I can post it if needed.

Answer

I think you forgot the <Type> in new Node:

Node<Type>* dynamicNode = new Node<Type>(indata); 

also, it is the best to use auto:

auto dynamicNode = new Node<Type>(indata); 
Comments