problem with class template

I have two header files - MyQueue.h and MyStack.h and I am getting an error that says
1
2
\assignment\assignment\mystack.h(36): error C2953: 'NodeType' : class template has already been defined
\assignment\assignment\myqueue.h(34) : see declaration of 'NodeType'

this is the code it brings me to when i click the error message for the stack
1
2
3
4
5
struct NodeType
{
   ItemType info;
   NodeType<ItemType>* next;
};

and this is the code from the queue error message
1
2
3
4
5
struct NodeType
{
    ItemType info;
    NodeType* next;
};

if i try to delete this code from one of the files more errors come up
kw1991 wrote:
if i try to delete this code from one of the files more errors come up

This means that it is used later in the header files.

Delete this structure from one of your .h files and then include the other header in this header.
thanks it worked. Maybe you could help with another problem?
I am trying to make a queue and i want to print out the values from the queue
this is what i have in main(this is just calling the function):
 
q.printQueue();

and heres the code for printQueue:
1
2
3
4
5
6
7
8
9
void QueType <ItemType>::printQueue()
{
	NodeType <ItemType> * tempPtr = qFront;
	while (tempPtr != Null)
	{
		cout << tempPtr -> info;
		tempPtr = tempPtr -> next;
	}
}

the error i'm getting is this:
1
2
3
4
5
6
7
8
9
10
11
1>          i:\assignment\assignment\myqueue.h(135) : while compiling class template member function 'void QueType<ItemType>::printQueue(void)'
1>          with
1>          [
1>              ItemType=int
1>          ]
1>          i:\assignment\assignment\assignment.cpp(10) : see reference to class template instantiation 'QueType<ItemType>' being compiled
1>          with
1>          [
1>              ItemType=int
1>          ]
1>i:\assignment\assignment\myqueue.h(137): fatal error C1903: unable to recover from previous error(s); stopping compilation
Did you put template <class ItemType> on the line before void QueType <ItemType>::printQueue()?
yes. sorry didnt copy over that line on the post so thats not the problem
Last edited on
any idea what the problem could be?
Did you define printQueue() in the header. It is a common mistake to define templates in source files.
yes i did that to. The program builds fine if i comment out this q.printQueue(); in main. maybe the problem is here? am i not calling the function right?
Trying to get some information out of the error message. What happens on line 10 in assignment.cpp? line 135 in myqueue.h is that the definition of the printQueue function?
line 10 is QueType<int>q;. this is for making the queue. none of the other functions i made had a problem with this line. line 135 is NodeType <ItemType> * tempPtr = qFront; and line 137 is while (tempPtr != Null)

thats all where all the error messages are bringing me. I'm guessing the code in printQueue is wrong?
can anyone help?
Last edited on
Can you show more complete code?
do you want all the code from the queue?
All the code or just some small potions of the code that still compiles with the error you have.
I solved it thanks for all the help. could you help me with another problem?
http://www.cplusplus.com/forum/beginner/59638/
Topic archived. No new replies allowed.