Results 1 to 4 of 4

Thread: operator<< and templates

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

    Default operator<< and templates

    Hello,
    how can I overload << with templates, please?

    Qt Code:
    1. template <class T = int, size_t N = 100>
    2. class Stack {
    3. T _data[N];
    4. size_t _index;
    5.  
    6. public:
    7. Stack() : _index(0) { }
    8. void push(const T& value) {
    9. if ( _index != N) _data[_index] = value;
    10. else printf("stack full\n");
    11. }
    12.  
    13.  
    14. friend std::ostream& operator<<(std::ostream& os, const Stack<T>& stack) {
    15. for (int i=0; i < N; ++i)
    16. os << _data[i] << " ";
    17. return os;
    18. }
    19. };
    To copy to clipboard, switch view to plain text mode 
    Qt Code:
    1. error C2597: illegal reference to non-static member 'Stack<>::_data'
    2. .....and others.....
    To copy to clipboard, switch view to plain text mode 
    Regards

  2. #2
    Join Date
    Oct 2007
    Location
    Munich, Bavaria
    Posts
    144
    Thanks
    1
    Thanked 19 Times in 19 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: operator<< and templates

    Hi,
    try this:
    1.) move the operator<< outside the class
    2.) add the template declaration to the operator (template <class T = int, size_t N = 100>)
    3.) fix the compiler error: _data is a member of stack

    Take care,

    Tom

  3. #3
    Join Date
    Jan 2006
    Location
    Napoli, Italy
    Posts
    621
    Thanks
    5
    Thanked 86 Times in 81 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: operator<< and templates

    Your code has two error:

    1) You cannot define friend function body in the class declaration

    2) You must declare friend function as template

    3) In push function you must increment _index

    The following code compile and run right

    Qt Code:
    1. #include <iosfwd>
    2.  
    3. template <class T = int, size_t N = 100>
    4. class Stack {
    5. T _data[N];
    6. size_t _index;
    7.  
    8. public:
    9. Stack() : _index(0) { }
    10. void push(const T& value) {
    11. if ( _index != N)
    12. {
    13. _data[_index] = value;
    14. ++_index;
    15. }
    16. else
    17. printf("stack full\n");
    18. }
    19.  
    20. template <typename TYPE, size_t SIZE>
    21. friend std::ostream& operator<<(std::ostream& os, const Stack<TYPE, SIZE>& stack);
    22. };
    23.  
    24. template <typename T, size_t N>
    25. std::ostream& operator<<(std::ostream& os, const Stack<T, N>& stack)
    26. {
    27. for (size_t i=0; i < stack._index; ++i)
    28. os << stack._data[i] << " ";
    29. return os;
    30. }
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. int main()
    2. {
    3. Stack<int, 10> st;
    4. st.push(2);
    5. st.push(5);
    6. st.push(3);
    7.  
    8. std::cout << st << std::endl;
    9.  
    10. return 0;
    11. }
    To copy to clipboard, switch view to plain text mode 
    A camel can go 14 days without drink,
    I can't!!!

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

    Default Re: operator<< and templates

    sorry,
    this code seems work properly; I missed "stack." in os << _data. why does it work?
    Qt Code:
    1. template <class T = int, size_t N = 3>
    2. class Stack {
    3. //enum { N = 100 };
    4. T _data[N];
    5. size_t _index;
    6.  
    7. public:
    8. Stack() : _index(0) { }
    9. void push(const T& value) {
    10. if ( _index != N) _data[_index++] = value;
    11. else printf("stack full\n");
    12. }
    13.  
    14. friend std::ostream& operator<< (std::ostream& os, const Stack<T>& stack) {
    15. for (int i=0; i < N; ++i)
    16. os << stack._data[i] << " ";
    17.  
    18. return os << endl;
    19. }
    20.  
    21. };
    To copy to clipboard, switch view to plain text mode 
    Regards

Similar Threads

  1. templates
    By mickey in forum General Programming
    Replies: 5
    Last Post: 16th January 2008, 15:25
  2. templates / Q_OBJECT question
    By _borker_ in forum Qt Programming
    Replies: 6
    Last Post: 19th December 2007, 20:35
  3. dynamic_cast and templates
    By KShots in forum General Programming
    Replies: 7
    Last Post: 7th August 2007, 20:01
  4. Templates vcapp and subdirs together
    By xgoan in forum Newbie
    Replies: 1
    Last Post: 24th August 2006, 10:46
  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.