Results 1 to 7 of 7

Thread: Operator overloading << error: must take exactly one argument

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    May 2011
    Posts
    22
    Qt products
    Qt4

    Question Operator overloading << error: must take exactly one argument

    Hi all!

    I can't get my head around the compile error I get when overloading the << operator:
    error: ‘QDataStream& Coordinate:perator<<(QDataStream&, const Coordinate&)’ must take exactly one argument

    Here is the code:

    coordinate.h:
    Qt Code:
    1. class Coordinate
    2. {
    3. public:
    4. Coordinate();
    5.  
    6. double x, y, z;
    7.  
    8. // output
    9. QDataStream &operator <<(QDataStream& out, const Coordinate& c);
    10. };
    To copy to clipboard, switch view to plain text mode 
    coordinate.cpp:
    Qt Code:
    1. #include "coordinate.h"
    2.  
    3. Coordinate::Coordinate()
    4. {
    5. x = y = z = 0.;
    6. }
    7.  
    8. QDataStream& operator <<(QDataStream& out, const Coordinate& c)
    9. {
    10. out << "(" << c.x << ", " << c.y << ", " << c.z << ")";
    11. return out;
    12. }
    To copy to clipboard, switch view to plain text mode 

    What is wrong / am I not getting?

    Thanks!
    Last edited by nomiz; 11th March 2012 at 12:54.

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

    Default Re: Operator overloading << error: must take exactly one argument

    The operator should be a standalone function and not a method of your class.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. #3
    Join Date
    May 2010
    Location
    Romania
    Posts
    1,021
    Thanks
    62
    Thanked 260 Times in 246 Posts
    Qt products
    Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: Operator overloading << error: must take exactly one argument

    If you want to make the data members private you can add friend operator inside your class:
    Qt Code:
    1. class Coordinate
    2. {
    3. public:
    4. Coordinate();
    5. // output
    6. friend QDataStream &operator <<(QDataStream& out, const Coordinate& c);
    7. private:
    8. double x, y, z;
    9. };
    To copy to clipboard, switch view to plain text mode 

  4. #4
    Join Date
    May 2011
    Posts
    22
    Qt products
    Qt4

    Default Re: Operator overloading << error: must take exactly one argument

    Quote Originally Posted by wysota View Post
    The operator should be a standalone function and not a method of your class.
    Worked! Thank you!

    Why does operator== not give any troubles?

    Quote Originally Posted by Zlatomir View Post
    If you want to make the data members private you can add friend operator inside your class:
    Ok, thanks for your info!
    Last edited by nomiz; 11th March 2012 at 15:42.

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

    Default Re: Operator overloading << error: must take exactly one argument

    Quote Originally Posted by nomiz View Post
    Worked! Thank you!

    Why does operator== not give any troubles?
    If you put the operator inside the class definition, then the operator takes the current object ("this") implicitly as its first parameter.

    So operator== comparing A against B can be placed in class A but not in B (or it can be a standalone function taking two parameters -- A and B). If you have a standalone operator << that takes QDataStream and MyStruct as its parameters, you could put it in QDataStream class but not in MyStruct class. Since in C++ you can't modify a class that has already been defined, you can't put it in QDataStream class and you're left with the standalone option.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  6. #6
    Join Date
    May 2011
    Posts
    22
    Qt products
    Qt4

    Default Re: Operator overloading << error: must take exactly one argument

    Aha! Thanks alot. I'll buy and read a proper C++ book

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

    Default Re: Operator overloading << error: must take exactly one argument

    That's usually a good idea
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


Similar Threads

  1. Overloading QMap << operator
    By The 11th plague of Egypt in forum Newbie
    Replies: 3
    Last Post: 14th September 2011, 19:24
  2. Operator Overloading
    By naturalpsychic in forum Newbie
    Replies: 1
    Last Post: 19th July 2011, 05:19
  3. SOLVED: Operator overloading QDataStream
    By eekhoorn12 in forum Qt Programming
    Replies: 1
    Last Post: 9th May 2010, 23:55
  4. QList Overloading operator==()
    By josepvr in forum Qt Programming
    Replies: 8
    Last Post: 28th January 2009, 15:28
  5. operator [] overloading
    By darksaga in forum General Programming
    Replies: 5
    Last Post: 8th April 2008, 15:27

Tags for this Thread

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
  •  
Qt is a trademark of The Qt Company.