Solved

C++: << Operator does not work as expected

Posted on 2010-11-16
7
386 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);

I added this to person.cpp
....
....
ostream &person::operator<<(ostream &stream) {
            stream << this->getAge()  << " ";
            return stream;
      }

In the main function, I did the following.
person P4 = person("C", "C", 23); //first name, last name and age
cout << &P4 << " ";

The code compiles fine but when I run the code, I see this output:
0012FDFC

I am expecting output:
23

I am using Visual Studio 2008.

Not sure what am I doing wrong. Please help. Thanks in advance.
0
Comment
Question by:shekhar_shashi
  • 4
  • 2
7 Comments
 
LVL 44

Assisted Solution

by:AndyAinscow
AndyAinscow earned 100 total points
ID: 34149200
&P4 means the memory adress of P4

does this do what you expect
cout << P4 << " ";
0
 
LVL 3

Author Comment

by:shekhar_shashi
ID: 34149276
If I change the code this way, I get this compilation error:

error C2679: binary '<<' : no operator found which takes a right-hand operand of type 'person' (or there is no acceptable conversion)      
0
 
LVL 86

Accepted Solution

by:
jkr earned 400 total points
ID: 34149422
That's what evilrix' point was in your last question - you better make that
class person; // foward declaration

ostream& operator<< ( ostream& os, const student& p) ;

class person
{
  friend ostream& operator<< ( ostream& os, const person& p) ;

  // ...

};

ostream& operator<< ( ostream& os, const person& p)
{
  os << p.getAge();

  return 0s;
}

Open in new window

0
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 
LVL 3

Author Comment

by:shekhar_shashi
ID: 34149717
Pardon my C++ ignorance here.

I am getting about 9 compilation errors after making these code changes.

Instead of posting all the compilation errors, let me ask a few questions:

Question 1:
Where should the forward declaration go?

class person; // foward declaration

ostream& operator<< ( ostream& os, const student& p) ;

Question 2: I am guessing this code needs to be placed in the header file.

class person
{
  friend ostream& operator<< ( ostream& os, const person& p) ;

  // ...

};

Am I correct?

Question 3:

I am guessing this code will be placed in the person.cpp file.

ostream& operator<< ( ostream& os, const person& p)
{
  os << p.getAge();

  return 0s;
}
 
Am I correct?

Thanks.


0
 
LVL 86

Assisted Solution

by:jkr
jkr earned 400 total points
ID: 34149837
1. Just above the operator's declaration

2. Actually, you just need to add
  friend ostream& operator<< ( ostream& os, const person& p) ;

to the declaration of the 'person' class.

3. You're absolutely correct ;o)
0
 
LVL 3

Author Comment

by:shekhar_shashi
ID: 34149879
Your answer was spot on.  Thanks a bunch for helping out this C++ newbie.
0
 
LVL 3

Author Closing Comment

by:shekhar_shashi
ID: 34149904
Thanks
0

Featured Post

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

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…
In Easy String Encryption Using CryptoAPI in C++ (http://www.experts-exchange.com/viewArticle.jsp?aid=1193) I described how to encrypt text and recommended that the encrypted text be stored as a series of hexadecimal digits -- because cyphertext may…
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 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.

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