"name followed by '::' must be a class or namespace name" error when making a template class

Viewed 4694

Node.cpp code :

#include "Node.h"
using namespace std;

template <typename t> 
Node :: Node(t nodeDate){
data = nodeData;

}

Node.h code :

#ifndef NODE_H
#define NODE_H

using namespace std;

template <typename t>
class Node{
public:
    Node(t nodeData);
    
private:
    Node *nextNode;
    type data;
friend class LinkedList;

    

};
#endif

Node :: Node(t nodeDate) in the cpp class is giving a "name followed by '::' must be a class or namespace name" and I'm not sure why since I have the include statement

1 Answers

The constructor Node is a data member of the class template Node<t>. So you need to write

template <typename t> 
Node<t> :: Node(t nodeDate){
data = nodeData;

}

And you need to initialize the data member nextNode.

It would be better to declare the constructor parameter as

Node( const t &nodeData);

The constructor definition can look the following way

template <typename t> 
Node<t> :: Node( const t &nodeDate) : data( nodeData ), nextNode( nullptr )
{
}

Also place the constructor definition in the header.

And it is unclear what the name type used in this data member declaration

type data;

means. Do you mean

t data;

Also it would be better if the class Node would be an internal class of the class LinkedList.

Related