ISO C++ forbids declaration of 'Node' with no type

Helo,

i'm trying to implement the singly-linked list data structure. The problem is, that i'm getting a good amount of error messages, which seem to be related to each other. The first message is this:

ISO C++ forbids declaration of 'Node' with no type

Here are the two files of the project:

ChainedList.h :

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#ifndef CHAINEDLIST_H
#define CHAINEDLIST_H

#include "Node.h"


class ChainedList
	{
		private:
	
		Node * Head;

		ChainedList()
			{
				Head = null;
			}


		public:

		void insertAfter(Node &node, Node &newNode) 
			{
     				newNode.Next = node.Next; 
     				node.Next = &newNode;   
			}

		void insertAfter(Node &newNode) //A seperate function is needed when inserting at the List's beginning
			{
				newNode.Next = Head;    
				Head = &newNode; 
			}


		void removeAfter(Node &node) 
			{
				Node * nodeBeingRemoved = node.Next; 
				node.next = node.Next.Next;   
				delete nodeBeingRemoved;     
			}

		void removeAfter() 
			{
				Node * nodeBeingRemoved = Head; 
				Head = Head.Next;   
				delete nodeBeingRemoved;     
			}

	};


#endif 



Node.h :

1
2
3
4
5
6
7
8
9
10
11
#ifndef NODE_H
#define NODE_H
		template <class T>
		class Node
			{
				public:
				T Data;
				Node * Next;
			};

#endif 
Node needs to be declared as Node<T> * Head; etc.
Node needs to be declared as Node<T> * Head; etc.


Then i get these errors:
'T' was not declared in this scope
template argument 1 is invalid

By the way, i post all the errors i get, maybe that will help.


/home/kane/Programs/C++/ChainedList/src/ChainedList.h:32: error: ISO C++ forbids declaration of 'Node' with no type
/home/kane/Programs/C++/ChainedList/src/ChainedList.h:32: error: expected ';' before '*' token
/home/kane/Programs/C++/ChainedList/src/ChainedList.h:43: error: 'Node' is not a type
/home/kane/Programs/C++/ChainedList/src/ChainedList.h:43: error: 'Node' is not a type
/home/kane/Programs/C++/ChainedList/src/ChainedList.h:49: error: 'Node' is not a type
/home/kane/Programs/C++/ChainedList/src/ChainedList.h:56: error: 'Node' is not a type
/home/kane/Programs/C++/ChainedList/src/ChainedList.h: In constructor 'ChainedList::ChainedList()':
/home/kane/Programs/C++/ChainedList/src/ChainedList.h:37: error: 'Head' was not declared in this scope
/home/kane/Programs/C++/ChainedList/src/ChainedList.h:37: error: 'null' was not declared in this scope
/home/kane/Programs/C++/ChainedList/src/ChainedList.h: In member function 'void ChainedList::insertAfter(int&, int&)':
/home/kane/Programs/C++/ChainedList/src/ChainedList.h:45: error: request for member 'Next' in 'newNode', which is of non-class type 'int'
make[2]: Nothing to be done for `all-am'.
/home/kane/Programs/C++/ChainedList/src/ChainedList.h:45: error: request for member 'Next' in 'node', which is of non-class type 'int'
/home/kane/Programs/C++/ChainedList/src/ChainedList.h:46: error: request for member 'Next' in 'node', which is of non-class type 'int'
/home/kane/Programs/C++/ChainedList/src/ChainedList.h: In member function 'void ChainedList::insertAfter(int&)':
/home/kane/Programs/C++/ChainedList/src/ChainedList.h:51: error: request for member 'Next' in 'newNode', which is of non-class type 'int'
/home/kane/Programs/C++/ChainedList/src/ChainedList.h:51: error: 'Head' was not declared in this scope
/home/kane/Programs/C++/ChainedList/src/ChainedList.h: In member function 'void ChainedList::removeAfter(int&)':
/home/kane/Programs/C++/ChainedList/src/ChainedList.h:58: error: missing template arguments before '*' token
/home/kane/Programs/C++/ChainedList/src/ChainedList.h:58: error: 'nodeBeingRemoved' was not declared in this scope
/home/kane/Programs/C++/ChainedList/src/ChainedList.h:58: error: request for member 'Next' in 'node', which is of non-class type 'int'
/home/kane/Programs/C++/ChainedList/src/ChainedList.h:59: error: request for member 'next' in 'node', which is of non-class type 'int'
/home/kane/Programs/C++/ChainedList/src/ChainedList.h:59: error: request for member 'Next' in 'node', which is of non-class type 'int'
/home/kane/Programs/C++/ChainedList/src/ChainedList.h:60: error: type '<type error>' argument given to 'delete', expected pointer
/home/kane/Programs/C++/ChainedList/src/ChainedList.h: In member function 'void ChainedList::removeAfter()':
/home/kane/Programs/C++/ChainedList/src/ChainedList.h:65: error: missing template arguments before '*' token
/home/kane/Programs/C++/ChainedList/src/ChainedList.h:65: error: 'nodeBeingRemoved' was not declared in this scope
/home/kane/Programs/C++/ChainedList/src/ChainedList.h:65: error: 'Head' was not declared in this scope
/home/kane/Programs/C++/ChainedList/src/ChainedList.h:67: error: type '<type error>' argument given to 'delete', expected pointer

Note: where in these errors it says line x, its really line x - 20, because there is a huge comment at the beginning of each file, which i didn't post.
class ChainedList

needs to be template <class T> class ChainedList

Then you need to define Node as Node<T>. You need to study templates some more.

Also, you realise your just coding your own implementation of an STL Container known as a 'vector' ?
I modified the things you said, and know most of the error messages are gone, except this:

Node.h:25: error: 'Node' is not a template

Also, you realise your just coding your own implementation of an STL Container known as a 'vector' ?


As far as i know, i'm trying to implement the linked list data type. I'm not sure what do you mean by vector.

Also, you are right about learning more about templates. Don't you know some good and long tutorial for templates with lots of examples, and perhaps exercises ? I was using this site's template tutorial, but i'm nut sure its enough for me.

Vector. It's a double-linked list implementation that can also be used like an array.

e.g
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>
#include <vector>

using namespace std;

int main() {
 vector<int> myList;

 myList.push_back(3);
 myList.push_back(4);
 myList.push_back(7);

 vector<int>::iterator ptr = myList.begin();
 while (ptr != myList.end()) {
  cout << "Entry: " << (*ptr) << endl;
  ptr++;
 }

 cout << "The 2nd element is: " << myList[1] << endl;

 return 0;
}


Don't you know some good and long tutorial for templates with lots of examples, and perhaps exercises ? I was using this site's template tutorial, but i'm nut sure its enough for me.


Google is your friend. But I would first check out the STL (Standard Template Library) - http://www.sgi.com/tech/stl/table_of_contents.html
Last edited on
Vector. It's a double-linked list implementation that can also be used like an array.


Well, this implementation is not doubly-linked.

I still don't know why am i getting this Node.h:25: error: 'Node' is not a template error message, although i searched with Google, hoping that maybe someone else had the same error.
Can you post your full code as it is now pls.
main.cpp is basically empty. It does nothing.


Node.h:

1
2
3
4
5
6
7
8
9
10
11
#ifndef NODE_H
#define NODE_H
		template <class T>
		class Node<T>
			{
				public:
				T Data;
				Node * Next;
			};

#endif 



ChainedList.h:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#ifndef CHAINEDLIST_H
#define CHAINEDLIST_H

#include "Node.h"

template <class T>
class ChainedList
	{
		private:

		Node<T> * Head;

		ChainedList()
			{
				Head = 0;
			}


		public:

		void insertAfter(Node<T> &node, Node<T> &newNode)
			{
     				newNode.Next = node.Next;
     				node.Next = &newNode;   
			}

		void insertAfter(Node<T> &newNode) 
			{
				newNode.Next = Head;    
				Head = &newNode; 
			}


		void removeAfter(Node<T> &node)
			{
				Node<T> * nodeBeingRemoved = node.Next; 
				node.next = node.Next.Next;   
				delete nodeBeingRemoved; 
			}

		void removeAfter() 
			{
				Node<T> * nodeBeingRemoved = Head; 
				Head = Head.Next;   
				delete nodeBeingRemoved;    
			}

	};


#endif 


1
2
3
4
5
6
7
8
9
10
11
#ifndef NODE_H
#define NODE_H
		template <class T>
		class Node<T>
			{
				public:
				T Data;
				Node * Next;
			};

#endif  


Shud have Node<T> * Next;
@kanesoban

Did you try with "typename" instead of "class" in the the T declaration of the template.
I currently dont have a c++ compiler on hand to try your code, any way, give a try with the "typename" in the template heading.
Good luck :)
Here's the fix

1
2
3
4
5
6
template< class T >
class Node                        // <--- do not put <T> here
{
  // ....
  Node* next;
};
Okay, now the program compiles... and the Chained List datatype seems to work too.
Topic archived. No new replies allowed.