2

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

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
  • 7
    **Don't** `using namespace std;` in a header file. It's bad enough anywhere, but it's really bad in a header. You also can't split your template code. Put it all in the header file. – sweenish Oct 21 '21 at 19:42
  • 6
    `Node::Node` -> `Node::Node`. Your code also has some typos... – HolyBlackCat Oct 21 '21 at 19:44
  • 4
    Regarding [splitting your template into .h and .cpp files](https://stackoverflow.com/questions/495021/why-can-templates-only-be-implemented-in-the-header-file) – Cory Kramer Oct 21 '21 at 19:44
  • 1
    Your error is rather literal. `Node ::` is not valid, because `Node` is not a class or namespace. It's a template. `Node` would be a class. – Drew Dormann Oct 21 '21 at 19:57
  • 1
    In addition to “Don’t `using namespace std;`”, there’s nothing in this code that uses anything from `std`, so the directive doesn’t accomplish anything. – Pete Becker Oct 21 '21 at 20:02

1 Answers1

1

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.

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335