Bitshift operator overloading help (weird)

This is a discussion on Bitshift operator overloading help (weird) within the C++ Programming forums, part of the General Programming Boards category; Hey, I'm working on a Solaris box and trying to overload the left bitshift operator as a "print" function in ...

  1. #1
    Registered User
    Join Date
    Aug 2010
    Location
    Rochester, NY
    Posts
    196

    Bitshift operator overloading help (weird)

    Hey,

    I'm working on a Solaris box and trying to overload the left bitshift operator as a "print" function in an object I have, so I can do:
    Code:
    std::cout << obj_name << std::endl;
    Every possible example I can find has 2 arguments, LHS, RHS. The prototype for this system appears to only have one, the RHS.

    If I add a second argument to the overloaded definition, I get:
    Code:
    file_name.h:53: error: `std::ostream& class_name::operator<<(std::ostream&, const class_name&)' must take exactly one argument
    I put a printf inside the overloaded operator and put:
    Code:
    obj << std::cout;
    And it prints the hardcoded string literal in the operator overloading..

    Does this mean I can only overload the RHS, or am I doing it wrong..

    I mean I think I could do it like:
    Code:
    std::cout << obj << std::cout
    Because it returns an ostream reference..


    Additional Notes:
    I didn't friend the overload, it's in the header in the class declaration.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,310
    Overload as a non-member function.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Aug 2010
    Location
    Rochester, NY
    Posts
    196
    Quote Originally Posted by laserlight View Post
    Overload as a non-member function.
    Hey,

    That works, one thing, though.

    I'm trying to put it in a header file (it just calls another function, it's a one liner), but then I geta linker error for multiple def, compiles fine if I put it in the cpp..but I'd hate to have a cpp just for one one line function. It's within the class sentry.

    Any way I can do that?

    NVM: I'm going to scrap the idea.

    Sorry about that, but I'm sure I would have asked the question eventually. Why did I scrap?

    Well when calling...
    Code:
    std::cout << *obj_name << std::endl;
    Looks a hell of a lot worse than:
    Code:
    obj_name->Display();
    Last edited by Syndacate; 01-12-2011 at 01:40 PM.

  4. #4
    Registered User NeonBlack's Avatar
    Join Date
    Nov 2007
    Posts
    435
    Quote Originally Posted by Syndacate View Post
    Hey,
    Sorry about that, but I'm sure I would have asked the question eventually. Why did I scrap?

    Well when calling...
    Code:
    std::cout << *obj_name << std::endl;
    Looks a hell of a lot worse than:
    Code:
    obj_name->Display();
    I would keep it. While your Display() function serves the single purpose of printing to std::cout, you can also use operator<< with other types of streams, such as file streams and stringstreams.
    I copied it from the last program in which I passed a parameter, which would have been pre-1989 I guess. - esbo

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,310
    Quote Originally Posted by Syndacate
    I'm trying to put it in a header file (it just calls another function, it's a one liner), but then I geta linker error for multiple def, compiles fine if I put it in the cpp..but I'd hate to have a cpp just for one one line function. It's within the class sentry.
    Declare the function to be inline, e.g.,
    Code:
    inline std::ostream& operator<<(std::ostream& out, const X& x)
    {
        return x.Display(out);
    }
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #6
    Registered User
    Join Date
    Aug 2010
    Location
    Rochester, NY
    Posts
    196
    Quote Originally Posted by NeonBlack View Post
    I would keep it. While your Display() function serves the single purpose of printing to std::cout, you can also use operator<< with other types of streams, such as file streams and stringstreams.
    If it was my own project, I'd say sure, but it's for a school project. It wasn't required to overload the operator, they didn't get even get to it yet - just figured it'd be convenient to just cout the puzzle, like how everything in java has a toString(), you can just cout the object...then I implemented it, and it didn't look as nice as just ptr->Display();. You know?

    Quote Originally Posted by laserlight View Post
    Declare the function to be inline, e.g.,
    Code:
    inline std::ostream& operator<<(std::ostream& out, const X& x)
    {
        return x.Display(out);
    }
    Ah, yeah, thanks. Not sure why I didn't think of that, it was inline when it was a member function...start to not think right after coding too long...lol.

  7. #7
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,622
    Quote Originally Posted by NeonBlack View Post
    I would keep it. While your Display() function serves the single purpose of printing to std::cout, you can also use operator<< with other types of streams, such as file streams and stringstreams.
    Well if you weren't going to use operator overloading, I think a Display function could be modified to work with an ostream argument, which gives you all the polymorphism you need to work with other types of streams.

    operator>> and operator<< are only a convention -- a very widely appreciated convention, but you could easily implement both to please everyone.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Weird problem with an array of vector<char *>'s ???
    By phummon in forum C++ Programming
    Replies: 2
    Last Post: 05-02-2010, 05:39 PM
  2. Weird loop keep happeneing....pls help debug...
    By Diamonddrago in forum C Programming
    Replies: 5
    Last Post: 11-18-2009, 10:51 PM
  3. weird things with my linked list of queue
    By -EquinoX- in forum C Programming
    Replies: 3
    Last Post: 11-22-2008, 10:23 PM
  4. weird
    By kiz in forum C Programming
    Replies: 8
    Last Post: 09-24-2007, 01:16 AM
  5. weird error
    By gandalf_bar in forum Linux Programming
    Replies: 2
    Last Post: 07-17-2005, 07:32 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21