Operator overload ostream

Hi, I dont know how to do operator overload for object with only array of 10 integers
Thanks

1
2
3
4
5
6
7
8
ostream& operator<<(ostream& out,MyObj & o)
    {
        for(int i;i<s;++i)
        out<<o.array[i]<<" ";
        return out;
    }

¿So what is the problem with that?
I dont know how to do it right,it gives me error that ostream must take exactly one argument
Could you please copy-paste the error, and the offending lines (with a little context)

By the way ostream& operator<<(ostream& out, const MyObj & o) you don't want to modify the object, ¿right?
Last edited on
No I dont modify the object
And the object is array of ten integers ,s is the size of the array


practice.cpp|20|error: 'std::ostream& MyObj::operator<<(std::ostream&, MyObj&)' must take exactly one argument|
\practice.cpp||In member function 'MyObj& MyObj::operator+(MyObj&)':|
practice.cpp|29|warning: reference to local variable 'temp' returned [enabled by default]|
||=== Build finished: 1 errors, 1 warnings ===|
If you're defining operator<< in your class you would do so like:
1
2
3
4
5
6
ostream& operator<<(ostream& o) const
{
          for (int i=0; i<s; ++i)
                    o << array[i] << " " ;
          return o;
}


then if you want to use it in the normal way, you'll also need a helper function outside the class definition:

1
2
3
4
inline ostream& operator<<(ostream&out, const MyObj& o)
{
          return o.operator<<(out) ;
}


Alternatively you can skip the definition of operator<< in the class and make the stand-alone function a friend of MyObj.
Thanks
I copy and paste your code but the error is the same
o.operator<<(out); kind of defeats the purpose of operator overloading, ¿don't you think?

cire wrote:
you'll also need a helper function outside the class definition: ostream& operator<<(ostream&out, const MyObj& o);
you can skip the definition of operator<< in the class and make the stand-alone function a friend
Supplying an overload defeats the purpose of operator overloading? No, I don't think so.
You have to skip the operator overloading the class and make it a friend.

You can't add methods to the ostream/istream classes, so you make them global friends:

friend ostream& operator<<(ostream& out, const myObj& obj);
@cire:
I meant that you don't use operators like o.operator<<(out);
You will simply o << out;
Why don't you? It's more clear than o << out. You aren't inserting out into o. All we were doing was forwarding to our overloaded class operator, and that's much clearer with an explicit call.
Last edited on
Then we will better with o.print_at(out);
Make the operators intuitive.
Don't change their semantic.
This is entirely a matter of style. No semantics were changed.

I would simply skip the member operator and go with the stand-alone. print_at is an inaccurate description. out could be a file stream, a console stream, a stringstream. Conceptually, we're inserting data into a stream, so if I went the named method route, I'd be more likely to use insert(out) taking an output iterator as the parameter type.

Topic archived. No new replies allowed.