C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Overloading Operator <<

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++)
View previous topic :: View next topic  
Author Message
jois.de.vivre@gmail.com
Guest





PostPosted: Fri Sep 16, 2005 11:10 pm    Post subject: Overloading Operator << Reply with quote



Hi, I'm having some trouble overloading the << operator. I have the
following, very simple code:

#include using namespace std;

class test
{
private:
int val;
public:
test():val(0){}
const int GetVal() const{
return val;
}

ostream& operator<< (ostream& os , test& a) {
os << a.GetVal();
return os;
}
};
//------End Sample Code

I get the following error during compile. What does this error mean?

error: 'std::ostream& test::operator<<(std::ostream&, test&)' must
take exactly one argument

I want to do something like

test MyTest;
cout << test << endl;

Thanks

Back to top
Thomas Tutone
Guest





PostPosted: Fri Sep 16, 2005 11:30 pm    Post subject: Re: Overloading Operator << Reply with quote




jois.de.vivre wrote:

Quote:
Hi, I'm having some trouble overloading the << operator. I have the
following, very simple code:

#include using namespace std;

class test
{
private:
int val;
public:
test():val(0){}
const int GetVal() const{
return val;
}

ostream& operator<< (ostream& os , test& a) {
os << a.GetVal();
return os;
}
};
//------End Sample Code

I get the following error during compile. What does this error mean?

error: 'std::ostream& test::operator<<(std::ostream&, test&)' must
take exactly one argument

operator<< can either be a member function and take one argument - an
ostream& - or it can be a nonmember function and take two arguments -
an ostream& and a const test&. You conflate the two - you both make it
a member function, but you also include two arguments. Hence the error
message. Choose one or the other. And typically people make it a
nonmember function, and I suggest you do the same.

By the way, make it const test&, not test&.

Best regards,

Tom


Back to top
Ian
Guest





PostPosted: Fri Sep 16, 2005 11:52 pm    Post subject: Re: Overloading Operator << Reply with quote



[email]jois.de.vivre (AT) gmail (DOT) com[/email] wrote:
Quote:

ostream& operator<< (ostream& os , test& a) {
os << a.GetVal();
return os;
}
};
//------End Sample Code

I get the following error during compile. What does this error mean?

error: 'std::ostream& test::operator<<(std::ostream&, test&)' must
take exactly one argument

It means the member operator<< must have one argument.


try

friend std::ostream& operator<<( std::ostream& os, const test& a)
{
os << a.GetVal();
return os;
}

Or if no private members are accessed, put the operator outside of the
class.

Ian

Back to top
Ali Çehreli
Guest





PostPosted: Sat Sep 17, 2005 12:17 am    Post subject: Re: Overloading Operator << Reply with quote

"Thomas Tutone" <Thomas8675309 (AT) yahoo (DOT) com> wrote

Quote:

jois.de.vivre wrote:

Hi, I'm having some trouble overloading the << operator. I have the
following, very simple code:

#include using namespace std;

class test
{
private:
int val;
public:
test():val(0){}
const int GetVal() const{
return val;
}

ostream& operator<< (ostream& os , test& a) {
os << a.GetVal();
return os;
}
};
//------End Sample Code

I get the following error during compile. What does this error mean?

error: 'std::ostream& test::operator<<(std::ostream&, test&)' must
take exactly one argument

operator<< can either be a member function and take one argument - an
ostream& - or it can be a nonmember function and take two arguments -
an ostream& and a const test&. You conflate the two - you both make it
a member function, but you also include two arguments. Hence the error
message. Choose one or the other. And typically people make it a
nonmember function, and I suggest you do the same.

Well, OP is probably trying to use it in this usual context:

test my_test;
cout << my_test;

In that case, operator<< must be a free function and take two arguments.

Also, regarding Ian's post, if needed, the 'friend' keyword goes with the
in-class declaration of the free function; not with the definition of it:

class test
{
friend ostream & operator<< (ostream &, test const &);

/* ... */
};

ostream & operator<< (ostream & os, test const & object)
{
return os << object.val;
}

Of course, since private access is not needed in the OP's case, it is better
to just define the free function:

class test
{
/* ... */
};

ostream & operator<< (ostream & os, test const & object)
{
return os << object.GetVal();
}

Ali


Back to top
Marcus Kwok
Guest





PostPosted: Thu Sep 29, 2005 3:10 pm    Post subject: Re: Overloading Operator << Reply with quote

Thomas Tutone <Thomas8675309 (AT) yahoo (DOT) com> wrote:
Quote:
operator<< can either be a member function and take one argument - an
ostream& - or it can be a nonmember function and take two arguments -
an ostream& and a const test&. You conflate the two - you both make it
a member function, but you also include two arguments. Hence the error
message. Choose one or the other. And typically people make it a
nonmember function, and I suggest you do the same.

I looked in the FAQ and did not find the answer to this question:

Why are the nonmember function versions preferred over the member
function versions?

--
Marcus Kwok

Back to top
Victor Bazarov
Guest





PostPosted: Thu Sep 29, 2005 3:48 pm    Post subject: Re: Overloading Operator << Reply with quote

Marcus Kwok wrote:
Quote:
Thomas Tutone <Thomas8675309 (AT) yahoo (DOT) com> wrote:

operator<< can either be a member function and take one argument - an
ostream& - or it can be a nonmember function and take two arguments -
an ostream& and a const test&. You conflate the two - you both make it
a member function, but you also include two arguments. Hence the error
message. Choose one or the other. And typically people make it a
nonmember function, and I suggest you do the same.


I looked in the FAQ and did not find the answer to this question:

Why are the nonmember function versions preferred over the member
function versions?

If you are trying to output _your_ class to an ostream object using
operator << (insertion into stream), then you simply *can't* have your
overloaded operator as a member because it has to be a member of the
'ostream', and you're not allowed to modify it. You're stuck with
implementing operator<< as non-member.

Now, as to why sometimes implementing two-operand operators is better
as non-member than a member, it's covered in Effective C++ book. And
you can only consider the reasons if you have a choice. In your case
you don't.

V

Back to top
Marcus Kwok
Guest





PostPosted: Thu Sep 29, 2005 6:21 pm    Post subject: Re: Overloading Operator << Reply with quote

Victor Bazarov <v.Abazarov (AT) comacast (DOT) net> wrote:
Quote:
Marcus Kwok wrote:
Thomas Tutone <Thomas8675309 (AT) yahoo (DOT) com> wrote:

operator<< can either be a member function and take one argument - an
ostream& - or it can be a nonmember function and take two arguments -
an ostream& and a const test&. You conflate the two - you both make it
a member function, but you also include two arguments. Hence the error
message. Choose one or the other. And typically people make it a
nonmember function, and I suggest you do the same.


I looked in the FAQ and did not find the answer to this question:

Why are the nonmember function versions preferred over the member
function versions?

If you are trying to output _your_ class to an ostream object using
operator << (insertion into stream), then you simply *can't* have your
overloaded operator as a member because it has to be a member of the
'ostream', and you're not allowed to modify it. You're stuck with
implementing operator<< as non-member.

Doesn't this contradict Thomas's first sentence above? What about the
member function

ostream& MyClass::operator<<(ostream&);


Will this not allow you to do, for example,

MyClass c;
std::cout << c;


or does the declaration imply

c << std::cout;
(which is backwards from the way it's supposed to be)?

Quote:
Now, as to why sometimes implementing two-operand operators is better
as non-member than a member, it's covered in Effective C++ book. And
you can only consider the reasons if you have a choice. In your case
you don't.

Thanks, I'll look into picking up that book; it seems to be very well
regarded.

--
Marcus Kwok

Back to top
Victor Bazarov
Guest





PostPosted: Thu Sep 29, 2005 6:39 pm    Post subject: Re: Overloading Operator << Reply with quote

Marcus Kwok wrote:
Quote:
Victor Bazarov <v.Abazarov (AT) comacast (DOT) net> wrote:

Marcus Kwok wrote:

Thomas Tutone <Thomas8675309 (AT) yahoo (DOT) com> wrote:


operator<< can either be a member function and take one argument - an
ostream& - or it can be a nonmember function and take two arguments -
an ostream& and a const test&. You conflate the two - you both make it
a member function, but you also include two arguments. Hence the error
message. Choose one or the other. And typically people make it a
nonmember function, and I suggest you do the same.


I looked in the FAQ and did not find the answer to this question:

Why are the nonmember function versions preferred over the member
function versions?

If you are trying to output _your_ class to an ostream object using
operator << (insertion into stream), then you simply *can't* have your
overloaded operator as a member because it has to be a member of the
'ostream', and you're not allowed to modify it. You're stuck with
implementing operator<< as non-member.


Doesn't this contradict Thomas's first sentence above? What about the
member function

ostream& MyClass::operator<<(ostream&);


Will this not allow you to do, for example,

MyClass c;
std::cout << c;

No. It would allow you to do this

MyClass c;
c << std::cout;

Quote:
or does the declaration imply

c << std::cout;
(which is backwards from the way it's supposed to be)?

Yes. Well, it's not backwards. The left operand of a binary member is
always the object of the class in which the operand is the member. You
could, for fun, define it as

ostream& MyClass::operator>>(ostream&);

and use it as

MyClass c;
c >> std::cout << std::endl;

A bit awkward, but some may find it making sense...

V

Back to top
Marcus Kwok
Guest





PostPosted: Thu Sep 29, 2005 7:07 pm    Post subject: Re: Overloading Operator << Reply with quote

Victor Bazarov <v.Abazarov (AT) comacast (DOT) net> wrote:
Quote:
Marcus Kwok wrote:
ostream& MyClass::operator<<(ostream&);


Will this not allow you to do, for example,

MyClass c;
std::cout << c;

No. It would allow you to do this

MyClass c;
c << std::cout;

or does the declaration imply

c << std::cout;
(which is backwards from the way it's supposed to be)?

Yes. Well, it's not backwards. The left operand of a binary member is
always the object of the class in which the operand is the member.

OK, I understand. Thanks.

Quote:
You could, for fun, define it as

ostream& MyClass::operator>>(ostream&);

and use it as

MyClass c;
c >> std::cout << std::endl;

A bit awkward, but some may find it making sense...

Maybe it will be useful when they start an Obfuscated C++ contest
(IOC++CC?) :)

--
Marcus Kwok

Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.