Passing Object by to queue

I've been working on a little project and hit a snag. I'm using nodes for a queue and stack class that were created using an existing list node class. I create an object for a student class and I want to enqueue that object.

1
2
3
4
5
6
7
8
9
10
int main()
{

Queue sLine;
Customer stu;
Queue<Student &> 

cLine.enqueue(cust); 

}


That's basically the coding of it in main. However when I follow the error which says uninitialized reference member ListNode<Student& info>::data;

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
#ifndef LISTND_H
#define LISTND_H

template< class T > class List;

template< class NODETYPE >
class ListNode {
   friend class List< NODETYPE >;
public:
   ListNode( const NODETYPE & );
   NODETYPE getData() const;
   void setNextPtr( ListNode *nPtr ) { nextPtr = nPtr; }
   ListNode *getNextPtr() const { return nextPtr; }
private:
   NODETYPE data;
   ListNode *nextPtr;
};


template< class NODETYPE >
ListNode< NODETYPE >::ListNode( const NODETYPE &info )
{
// The error being referenced is here!!
   data = info;
   nextPtr = 0;
}


template< class NODETYPE >
NODETYPE ListNode< NODETYPE >::getData() const { return data; }

#endif 


Anyone have any advice on what I may have been doing wrong? Trying to work within certain contexts.
Last edited on
I doubt if a queue of references is really what you want, but if you must try it change the definition of the constructor of ListNode to:

1
2
template <class NODETYPE>
ListNode<NODETYPE>::ListNode(const NODETYPE& info) : data(info), nextPtr(nullptr) {}


(Which is the form you should prefer anyway.)

http://www.parashift.com/c++-faq/init-lists.html
Last edited on
Ahh thanks, and the error was in my Student file constructor where I initialized the data members to null... the joys of being forced to not use string.
Topic archived. No new replies allowed.