?
Solved

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

Posted on 2010-11-16
4
Medium Priority
?
598 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 1200 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 800 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

Industry Leaders: 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

This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
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.
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.
Suggested Courses

770 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