Results 1 to 7 of 7

Thread: problem using template

  1. #1
    Join Date
    Jan 2006
    Posts
    976
    Thanks
    53
    Qt products
    Qt3
    Platforms
    Windows

    Default problem using template

    hi, I'm coding the queue of Primer book (template chapter). but I've a prob.
    Qt Code:
    1. error C2440: '=' : cannot convert from 'QueueItem<Type> *' to 'int *'
    2. with
    3. [
    4. Type=int
    5. ]
    To copy to clipboard, switch view to plain text mode 
    Qt Code:
    1. template <class Type> class Queue;
    2.  
    3. template <class Type>
    4. class QueueItem {
    5. private:
    6. friend class Queue<Type>;
    7. public:
    8.  
    9. Type value;
    10. Type* next;
    11. QueueItem(const Type& t) : value(t) {next=0;}
    12. ~QueueItem();
    13. };
    14. template <class Type>
    15. class Queue {
    16. private:
    17. QueueItem<Type>* tail;
    18. QueueItem<Type>* head;
    19. public:
    20. Type& remove();
    21. void insert(const Type&);
    22. void print();
    23. bool empty() const {return (head == 0);}
    24. Queue();
    25. ~Queue();
    26. };
    27.  
    28. template <class Type>
    29. void Queue<Type>::insert(const Type& value) {
    30. QueueItem<Type>* p = new QueueItem<Type>(value);
    31. tail->next = p; //here problem
    32. tail = p;
    33. }
    To copy to clipboard, switch view to plain text mode 
    How to solve it? Type should be int when i'm go inside insert method.....
    thanks
    Regards

  2. #2
    Join Date
    Nov 2006
    Location
    Almaty, Kazakhstan
    Posts
    8
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: problem using template

    you should replace the 10th line with
    Qt Code:
    1. QueueItem<Type>* next;
    To copy to clipboard, switch view to plain text mode 

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

  3. #3
    Join Date
    Jan 2006
    Posts
    976
    Thanks
    53
    Qt products
    Qt3
    Platforms
    Windows

    Default Re: problem using template

    thanks, but in this case I've other type of problem:
    Qt Code:
    1. error C2059: syntax error : '<'
    2. error C2238: unexpected token(s) preceding ';'
    To copy to clipboard, switch view to plain text mode 
    Qt Code:
    1. template <class Type>
    2. class Queue {
    3. private:
    4. class QueueItem {
    5. private:
    6.  
    7. public:
    8. Type value;
    9. QueueItem<Type>* next; //problem
    10. QueueItem(Type t) : value(t) {next=0;}
    11. ~QueueItem();
    12. };
    To copy to clipboard, switch view to plain text mode 
    Regards

  4. #4
    Join Date
    Jan 2006
    Posts
    976
    Thanks
    53
    Qt products
    Qt3
    Platforms
    Windows

    Default Re: problem using template

    Qt Code:
    1. QueueItem* next;
    To copy to clipboard, switch view to plain text mode 
    Regards

  5. #5
    Join Date
    Jan 2006
    Posts
    976
    Thanks
    53
    Qt products
    Qt3
    Platforms
    Windows

    Default Re: problem using template

    hi Do anyone know how pass Queue (by referece) to a funzion (in these 2 case)? thanks
    Qt Code:
    1. void populate(Queue<int>& ll){
    2.  
    3. }
    4. Queuet<int,20>* l = new Queue<int,20>;
    5. //Queue<int>* l = new Queue<int>;
    6. populate(l);
    To copy to clipboard, switch view to plain text mode 
    Regards

  6. #6
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,518 Times in 1,388 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: problem using template

    Qt Code:
    1. populate(*l);
    To copy to clipboard, switch view to plain text mode 
    J-P Nurmi

  7. #7
    Join Date
    Jan 2006
    Posts
    976
    Thanks
    53
    Qt products
    Qt3
    Platforms
    Windows

    Default Re: problem using template

    1.
    Qt Code:
    1. error C2664: 'populate' : cannot convert parameter 1 from 'Queue<Type,size>' to 'Queue<Type> &'
    2. with
    3. [
    4. Type=int,
    5. size=20
    6. ]
    7. and
    8. [
    9. Type=int
    10. ]
    To copy to clipboard, switch view to plain text mode 
    2. Could be possible define my own iterator on the queue in the way that I can do this:
    Qt Code:
    1. Queue<int,20>::_iterator it;
    To copy to clipboard, switch view to plain text mode 
    Regards

Similar Threads

  1. QTimer problem ... it runs but never triggs
    By yellowmat in forum Newbie
    Replies: 4
    Last Post: 4th July 2006, 12:54
  2. Grid Layout Problem
    By Seema Rao in forum Qt Programming
    Replies: 2
    Last Post: 4th May 2006, 12:45
  3. Problem with bitBlt
    By yellowmat in forum Newbie
    Replies: 1
    Last Post: 5th April 2006, 14:08
  4. fftw problem
    By lordy in forum General Programming
    Replies: 1
    Last Post: 16th March 2006, 21:36
  5. Replies: 16
    Last Post: 7th March 2006, 15:57

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.