0

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.

kdubs
  • 936
  • 3
  • 22
  • 45
  • Are you actually getting a compiler error, or is this simply coming from Intellisense? – Nathan Monteleone Nov 17 '16 at 17:19
  • @NathanMonteleone yes I'm getting a compiler error. The compiler error is `error C2514: 'Node': class has no constructors` – kdubs Nov 17 '16 at 17:25
  • @pm100 Not sure if it is unique to VS. If I don't include it in the header file then theoretically the .h won't be able to "see" the .tem file. Although it doesn't seem to see it anyways. – kdubs Nov 17 '16 at 17:31
  • Have you tried compiling the two files from command line?If you had what was the error there? – theVoid Nov 17 '16 at 17:56
  • Yeah I think we might need to see your main... – Nathan Monteleone Nov 17 '16 at 18:01
  • @NathanMonteleone is it acceptable to add a link to a drive folder where all of the project files are stored? The main needs my other class files to run. – kdubs Nov 17 '16 at 18:04
  • Really all that's needed is to see how you're including Node.h, and how you're actually attempting to use it. – Nathan Monteleone Nov 17 '16 at 18:26
  • Just tested it, and your code as is should work with MSVC. Wrote a simple program that `#include`s `"node.h"`, and defines `main()` as `int main() { Node nint(29); }`. Try using that, and see if it compiles. – Justin Time - Reinstate Monica Nov 17 '16 at 18:40
  • @JustinTime hmm it does seem to work when I separate it from my project and put it in it's own space. That is very strange. – kdubs Nov 17 '16 at 21:53
  • @JustinTime I updated the description with the new errors I am getting. – kdubs Nov 17 '16 at 22:10
  • Simple tests like that can be useful if you want to check whether a class works as is and the problem is somewhere else, or if the problem is in the class itself. – Justin Time - Reinstate Monica Nov 18 '16 at 19:46

1 Answers1

2

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); 
David Haim
  • 25,446
  • 3
  • 44
  • 78