PDA

View Full Version : problem using template



mickey
18th November 2006, 02:37
hi, I'm coding the queue of Primer book (template chapter). but I've a prob.


error C2440: '=' : cannot convert from 'QueueItem<Type> *' to 'int *'
with
[
Type=int
]



template <class Type> class Queue;

template <class Type>
class QueueItem {
private:
friend class Queue<Type>;
public:

Type value;
Type* next;
QueueItem(const Type& t) : value(t) {next=0;}
~QueueItem();
};
template <class Type>
class Queue {
private:
QueueItem<Type>* tail;
QueueItem<Type>* head;
public:
Type& remove();
void insert(const Type&);
void print();
bool empty() const {return (head == 0);}
Queue();
~Queue();
};

template <class Type>
void Queue<Type>::insert(const Type& value) {
QueueItem<Type>* p = new QueueItem<Type>(value);
tail->next = p; //here problem
tail = p;
}

How to solve it? Type should be int when i'm go inside insert method.....
thanks

fritz
18th November 2006, 04:29
you should replace the 10th line with


QueueItem<Type>* next;


because the pointer to the next node should be of type QueueItem<Type>, not of type of its value.

mickey
18th November 2006, 11:14
thanks, but in this case I've other type of problem:


error C2059: syntax error : '<'
error C2238: unexpected token(s) preceding ';'



template <class Type>
class Queue {
private:
class QueueItem {
private:

public:
Type value;
QueueItem<Type>* next; //problem
QueueItem(Type t) : value(t) {next=0;}
~QueueItem();
};

mickey
18th November 2006, 13:55
QueueItem* next;

mickey
18th November 2006, 15:18
hi Do anyone know how pass Queue (by referece) to a funzion (in these 2 case)? thanks


void populate(Queue<int>& ll){

}
Queuet<int,20>* l = new Queue<int,20>;
//Queue<int>* l = new Queue<int>;
populate(l);

jpn
18th November 2006, 15:22
populate(*l);

mickey
18th November 2006, 15:57
1.


error C2664: 'populate' : cannot convert parameter 1 from 'Queue<Type,size>' to 'Queue<Type> &'
with
[
Type=int,
size=20
]
and
[
Type=int
]

2. Could be possible define my own iterator on the queue in the way that I can do this:


Queue<int,20>::_iterator it;