Solved

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

Posted on 2010-11-16
4
576 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
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
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 additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

920 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

16 Experts available now in Live!

Get 1:1 Help Now