Solved

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

Posted on 2010-11-16
4
586 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 86

Accepted Solution

by:
jkr earned 300 total points
ID: 34147645
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
ID: 34147707
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
ID: 34148831
Thanks
0
 
LVL 3

Author Comment

by:shekhar_shashi
ID: 34148929
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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

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…
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

726 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