Results 1 to 7 of 7

Thread: Templates Confusion

  1. #1
    Join Date
    Aug 2007
    Posts
    275
    Thanks
    28
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Templates Confusion

    Guys I have this simple code on templates
    Qt Code:
    1. #include <iostream.h>
    2. #include <vector>
    3.  
    4. template <typename T>
    5. class MyQueue
    6. {
    7. std::vector<T> data;
    8. public:
    9. void Add(T const &);
    10. void Remove();
    11. void Print();
    12. };
    13.  
    14. template <typename T> void MyQueue<T> ::Add(T const &d)
    15. {
    16. data.push_back(d);
    17. }
    18.  
    19. template <typename T> void MyQueue<T>::Remove()
    20. {
    21. data.erase(data.begin( ) + 0,data.begin( ) + 1);
    22. }
    23.  
    24. template <typename T> void MyQueue<T>::Print()
    25. {
    26. std::vector <double>::iterator It1;
    27. It1 = data.begin();
    28. for ( It1 = data.begin( ) ; It1 != data.end( ) ; It1++ )
    29. cout << " " << *It1<<endl;
    30.  
    31. }
    32.  
    33.  
    34. int main (int argc,char *argv[])
    35. {
    36. MyQueue<double> q;
    37. q.Add(1.34);
    38. q.Add(2.24);
    39.  
    40. cout<<"Before removing data"<<endl;
    41. q.Print();
    42.  
    43. q.Remove();
    44. cout<<"After removing data"<<endl;
    45. q.Print();
    46. return 0;
    47. }
    To copy to clipboard, switch view to plain text mode 
    in my
    template <typename T> void MyQueue<T>::Print()
    method I want this iterator to be synchronize with my typename T
    Qt Code:
    1. std::vector <double>::iterator It1;
    To copy to clipboard, switch view to plain text mode 
    meaning if i say
    Qt Code:
    1. MyQueue<int> q;
    To copy to clipboard, switch view to plain text mode 
    the iterator should adjust himself to int, right now its always expecting a double.
    Please help,
    baray98

  2. #2
    Join Date
    May 2008
    Location
    Kyiv, Ukraine
    Posts
    418
    Thanks
    1
    Thanked 29 Times in 27 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Templates Confusion

    Wouldn't it be right to have std::vector <T>::iterator It1; ? Or am i missing something?
    I'm a rebel in the S.D.G.

  3. #3
    Join Date
    Aug 2007
    Posts
    275
    Thanks
    28
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Templates Confusion

    I think you are missing something. The way that i can think of specialized template where if
    T (in my case) is not double , then do something like this on the print method
    Qt Code:
    1. template <typename T> void MyQueue <T>::Print()
    2. {
    3. std::vector <double>::iterator It1;
    4. It1 = data.begin();
    5. for ( It1 = data.begin( ) ; It1 != data.end( ) ; It1++ )
    6. cout << " " << *It1<<endl;
    7. }
    8. template <> void MyQueue<long>::Print()
    9. {
    10. std::vector <long>::iterator It1;
    11. It1 = data.begin();
    12. for ( It1 = data.begin( ) ; It1 != data.end( ) ; It1++ )
    13. cout << " " << *It1<<endl;
    14.  
    15.  
    16. }
    17. template <> void MyQueue<int>::Print()
    18. {
    19. std::vector <int>::iterator It1;
    20. It1 = data.begin();
    21. for ( It1 = data.begin( ) ; It1 != data.end( ) ; It1++ )
    22. cout << " " << *It1<<endl;
    23. }
    24. template <> void MyQueue<bool>::Print()
    25. {
    26. std::vector <bool>::iterator It1;
    27. It1 = data.begin();
    28. for ( It1 = data.begin( ) ; It1 != data.end( ) ; It1++ )
    29. cout <<std::boolalpha << *It1<<endl;
    30. }
    To copy to clipboard, switch view to plain text mode 

    but now I want to extract the real printing itself and have just one method that will do this

    Qt Code:
    1. // this is the real printing part
    2. It1 = data.begin();
    3. for ( It1 = data.begin( ) ; It1 != data.end( ) ; It1++ )
    4. cout <<std::boolalpha << *It1<<endl;
    To copy to clipboard, switch view to plain text mode 

    how can i have just one method to do all this on all type of T

    baray98

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Templates Confusion

    What exactly is the problem? Is there an error message you could show us?

  5. #5
    Join Date
    Aug 2007
    Posts
    275
    Thanks
    28
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Templates Confusion

    My problem is that in my class template method
    Qt Code:
    1. template <typename T> void MyQueue<T>::Print(){
    2. std::vector <double>::iterator It1;
    3. It1 = data.begin();
    4. for ( It1 = data.begin( ) ; It1 != data.end( ) ; It1++ )
    5. cout << " " << *It1<<endl;
    6. }
    To copy to clipboard, switch view to plain text mode 

    this will only work for doubles since It1 is instantiated with doubles. I wanted to change this so that it will work on any T.

    and the only thing i can think of is using special template and give each type a print method

    baray98
    Last edited by jacek; 23rd November 2008 at 20:41. Reason: added missing line breaks

  6. #6
    Join Date
    Dec 2006
    Posts
    849
    Thanks
    6
    Thanked 163 Times in 151 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Templates Confusion

    replace
    Qt Code:
    1. std::vector <double>::iterator It1;
    To copy to clipboard, switch view to plain text mode 
    by
    Qt Code:
    1. typename std::vector<T>::iterator It1;
    2. // Or make it a const_iterator since you do not want to modify things.
    3. // For the same reason your method could (should) be declared const.
    To copy to clipboard, switch view to plain text mode 

    (You might want to provide a typedef for that in your class.)

    HTH

  7. #7
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Templates Confusion

    If you want something to adjust "on the fly" then you can't do it with templates. What you want can probably be done with subclassing and virtual methods.

Similar Threads

  1. close() function confusion
    By iamjayanth in forum Qt Programming
    Replies: 3
    Last Post: 13th November 2008, 10:45
  2. Little confusion Regarding general Initilizaion..!!!
    By kunalnandi in forum General Programming
    Replies: 19
    Last Post: 11th October 2008, 12:29
  3. Little confusion Regarding general Initilizaion.. :confused:
    By kunalnandi in forum General Programming
    Replies: 2
    Last Post: 19th September 2008, 10:06
  4. dynamic_cast and templates
    By KShots in forum General Programming
    Replies: 7
    Last Post: 7th August 2007, 20:01
  5. Templates : Help Please
    By sunil.thaha in forum General Programming
    Replies: 4
    Last Post: 14th February 2006, 13:50

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.