Solved

C++ with VS 2008: Unable to overload << Operator

Posted on 2010-11-16
4
571 Views
Last Modified: 2012-05-10
I am trying to overload the << operator.

I have a custom class called person.

I added this to person.h:

public:
.....
....
ostream &person::operator<<(ostream &stream, person &pers);


I added this to person.cpp
....
....
ostream &person::operator<<(ostream &stream, person &pers) {
            return stream;
      }

In the main function, I did the following.
person P4 = person("C", "C", 23);
cout << &P4 << " ";

I am getting an error that reads like:

error C2804: binary 'operator <<' has too many parameters.

I am using Visual Studio 2008.

Please help. Thanks in advance.
0
Comment
Question by:shekhar_shashi
  • 2
4 Comments
 
LVL 86

Accepted Solution

by:
jkr earned 300 total points
Comment Utility
If you added this as a member function, it indeed has one suprefluous parameter, it should be

ostream &person::operator<<(ostream &stream) {
            return stream;
      }

only. The 'person' argument you added is '*this' in your case. See also http://msdn.microsoft.com/en-us/library/1zy85x1e%28v=VS.90%29.aspx ("Compiler Error C2804")
0
 
LVL 40

Assisted Solution

by:evilrix
evilrix earned 200 total points
Comment Utility
Note, generally streaming operators should be free standing functions otherwise the calling syntax will be wrong. To call << as a class member you would have to do this...

person << cout;

when it should be

cout << person

so you need a free standing function with this signature...

ostream & operator<<(ostream &stream, person const & objPerson) ;
0
 
LVL 3

Author Closing Comment

by:shekhar_shashi
Comment Utility
Thanks
0
 
LVL 3

Author Comment

by:shekhar_shashi
Comment Utility
Since you already answered my question, I am closing this post. But I am running into another problem. So I'll post the next problem shortly.
0

Featured Post

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
In days of old, returning something by value from a function in C++ was necessarily avoided because it would, invariably, involve one or even two copies of the object being created and potentially costly calls to a copy-constructor and destructor. A…
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now