kdubs kdubs - 1 year ago 67
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
Node(Type indata);

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

#include "Node.tem"

and my Node.tem file looks like this:

template <class Type>
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
'Node': use of class template requires template argument list

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

Answer Source

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); 
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download