Results 1 to 6 of 6

Thread: templates

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

    Default templates

    Hello, I have some question:
    Qt Code:
    1. template<class T>
    2. void swap(T* a, T* b) {
    3. T temp = *a;
    4. *a = *b;
    5. *b = temp;
    6. }
    7. int x,y;
    8. swap(&x,&y);
    9. swap(x,y); //why this compile and it works?
    To copy to clipboard, switch view to plain text mode 
    Qt Code:
    1. template<class T>
    2. void swap(T* a, T* b) {
    3. T* temp = *a;
    4. *a = *b;
    5. *b = temp; // *b = *temp; /*with this same problem before */
    6. }
    7. int x,y;
    8. swap(&x, &y); //why now this doens't compile?
    9. swap(x, y); // why this compile
    To copy to clipboard, switch view to plain text mode 
    For what I knew, with void swap(T* a, T* b), should only work swap(&x, &y);

    Thanks in advance.
    Regards

  2. #2
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: templates

    Compiling with msvc, right?
    You don't get any error because the compiler uses this function instead, defined in include/utility.

    Qt Code:
    1. template<class _Ty> inline
    2. void swap(_Ty& _Left, _Ty& _Right)
    3. { // exchange values stored at _Left and _Right
    4. if (&_Left != &_Right)
    5. { // different, worth swapping
    6. _Ty _Tmp = _Left;
    7.  
    8. _Left = _Right;
    9. _Right = _Tmp;
    10. }
    11. }
    To copy to clipboard, switch view to plain text mode 

    Rename your function and you'll see the errors.

  3. #3
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: templates

    Do you have "using namespace std" anywhere in your source code?

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

    Default Re: templates

    Quote Originally Posted by jacek View Post
    Do you have "using namespace std" anywhere in your source code?
    yes, I have only main.cpp and I have using namespace std;
    But with Marcel hint, I solve every problem....but for me this is strange! Something more about avoid this problem? (apart change name to functions)
    It gets me the same error with:
    Qt Code:
    1. template<class T>
    2. T min(const T& a, const T& b ) {
    3. if( a < b )
    4. return( a ) ;
    5. else
    6. return( b ) ;
    7. }
    To copy to clipboard, switch view to plain text mode 
    but change to 'mymin' everything seems to work....
    Regards

  5. #5
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: templates

    Yes, these functions are already defined in utility.h, which gets included before your header.
    Try defining your functions in a separate namespace and access them with the scope resolution operator(myNameSpace::min or myNameSpace::swap).

  6. The following user says thank you to marcel for this useful post:

    mickey (16th January 2008)

  7. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: templates

    Quote Originally Posted by mickey View Post
    yes, I have only main.cpp and I have using namespace std;
    You wouldn't have such problems without that line. It's better to use std:: prefix for all functions and classes from standard namespace.

  8. The following user says thank you to jacek for this useful post:

    mickey (16th January 2008)

Similar Threads

  1. templates / Q_OBJECT question
    By _borker_ in forum Qt Programming
    Replies: 6
    Last Post: 19th December 2007, 20:35
  2. dynamic_cast and templates
    By KShots in forum General Programming
    Replies: 7
    Last Post: 7th August 2007, 20:01
  3. templates and Q_OBJECT
    By TheKedge in forum General Programming
    Replies: 12
    Last Post: 15th September 2006, 05:41
  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.