Solved

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

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

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

Suggested Solutions

Title # Comments Views Activity
returning a dereferenced pts in C++ 10 171
C language IDE – Compilers installation 14 89
Should CArray be used for a list of pointers in C++? 19 141
Unresolved External Symbols 3 151
Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
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 …
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

738 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