Class Overloading ostream Operators

Alright guys, I've been trying to help a friend out with a small side project and have never overloaded the ostream operators before and figured this would be a good time to learn. I have tried numerous ways to do this, but none of which have succeeded. I constantly get 1-2 errors and I know it's relevant to my new ostream overloading.

Here is a sample of the code:
std::ostream &Contact::operator << (std::ostream &out) {

I know it's not everything, but that should be enough to show you what I'm trying to do.

Essentially, I want to be able to cout/fileout the Contact object nicely (i.e. cout << newContact or fout << newContact) and have the class do all of the heavy lifting (that's what it's all about right?). I know I'm probably overlooking something small, but since this is my first time, I'm trying to make sure I understand it as well.

Any help is greatly appreciated, and if you need more code, I'll share it.

Edit: I should mention, I've been googling this for some time now, possibly with the wrong keywords, but I haven't found anything that was pointing me into the right direction. I have tried about 20 different variations of the above code and all of which seem to get me close, but then doesn't work.
Last edited on
Try declaring as a friend.
1
2
3
4
5
ostream& operator<<(ostream &out, Contact &info)
{
	out<<info;
	return out;
} 

This must be a global function, so no scope Contact:: is needed.
naraku9333
Try declaring as a friend.
Well since I'm specifying which namespace I'm using, I prepended std:: to the ostream like so:
std::ostream &operator << (std::ostream &out, Contact &c) {

And I get this error:
Code Blocks Compiler wrote:
C:\Programming\..\lib\contact.h|60|error: 'std::ostream& Contact::operator<<(std::ostream&, Contact&)' must take exactly one argument|
Last edited on
I know this isn't this hard. What am I missing now?
The << operator must be global. It can't be part of your Contact class.
make it global, no Contact:: Since your declaration looks OK, make sure you erased the prorotype from the class!
Thanks for the quick responses. I didn't think this should be as difficult as I was making it, but nothing was working. Here is part of my final code:
1
2
3
4
         friend std::ostream &operator << (std::ostream &out, Contact &c) {
            out << c.GetName();
            return out;
         }


I left it in the class declaration just because I like it there. I am slowly learning more about friendships and inheritances, but this works perfectly.
I started messing around with the input operator since the output seemed easy enough. Weird thing though, I couldn't find a reference explaining using the >> operator in a class anywhere. Is it possible?

Anyways, here is what I've gotten so far:
1
2
3
4
friend std::istream &operator >> (std::istream &in, Contact &c) {
            in >> c.contactName >>  "\n"
               >> c.contactPhone >> "\n"
               // ... 
Last edited on
in >> c.contactName >> "\n"

This is wrong.

The >> operator extracts something from the stream, and writes it in whatever variable is on the right side of the >>. You are putting "\n" on the right side of >>. The program cannot write data to "\n" because it is not a variable.
Last edited on
I had it as in.getline, but it didn't fix my error. I must have typed it wrong prior, but now the only error I'm getting, is this one:
C:\Programming\contact.h|78|error: no match for 'operator>>' in 'std::operator>><char, std::char_traits<char>, std::allocator<char> >((* & in), (* & c.Contact::contactName)) >> "\012"'|


Also, do I need to return the istream/ostream object, or can I return the Contact object?
...err... right. That error is telling you exactly what I just did. You can't write information to a constant.

Doing this:
 
in >> "\n";  // you can't extract something to a constant 

is like doing this:
 
"\n" = 5;  // just like you can't assign a constant 


It doesn't make any sense. That's why you're getting the error.


Also, do I need to return the istream/ostream object, or can I return the Contact object?


It wouldn't make any sense to return a Contact object, but you could if you wanted to.

The whole thing with returning an object let's you chain the >> calls. For example:

 
in >> a >> b;


How this works is... in >> a is evaluated first, then whatever that operator returns is used in the following expression:

 
whatever_in_a_returned >> b;


So logically, to make that work how you'd expect, you'd want to return a reference to 'in'.

In other words:
1
2
3
4
5
6
7
8
9
10
// in >> a >> b
//  if '>>' returns a reference to 'in', it's the same as doing this:
in >> a;
in >> b;


// in >> a >> b
//  if '>>' returns a reference to 'a', it's the same as doing this:
in >> a;
a >> b;
I keep forgetting to save my header file before I build. Apparently, I've had it right for the last several hours, but it was never saved so it was reading my original issue with it. And I don't know why I forgot that the cin statement returned the value it just received, but I didn't know it would work the same way with classes.

My last question is that I'm using this mainly for reading from a file, so the person I'm helping this with wants the file to contain certain text to help display the output better, for example:
John E. Doe
========
Phone: (123) 456-7890
Address: 12345 Main Street
City: Fakey State: US ZIP: 12345


How could you format the input operator to discard the "Phone:", "======", etc.? Would in.ignore(...) work?
Last edited on
Topic archived. No new replies allowed.