C++ operator overloading

Joined
May 19, 2007
Messages
8
I need to create a function called PrintFlightManifest. This function asks the user for a flight number (1 or 2), then the function prints all the passengers on that flight (no more than 20 passenger on each flight). I must use the overloaded >> operator.

This is the definition I have in my header file
Code:
friend ostream &operator<<(ostream &,Air &);

Air is the structure.

I have 2 questions:
1. Is the code above even correct?
2. Can someone give me at least a hint on how to even begin to write this code?

I wrote a code for the << operator. Is that at least correct?

Code:
ostream &operator<<(ostream &out, Air &getpass)
{
	Air *current = new Air;
	for(current = getpass.head; current != NULL; current = current->next)
	{
		out << current->person <<endl; 
	}
	return out;
}

Any help would be greatly appreciated.
 
Colossusx10 said:
1. Is the code above even correct?
It'll compile. Is it what you really want? Hard to tell.

Colossusx10 said:
2. Can someone give me at least a hint on how to even begin to write this code?
I don't think you mean to allocate a new Air object there. Since you haven't shown us a definition for Air, we can't guess if it's correct or not.
 
From my understanding, the second parameter (you call it getpass) is a reference to the object on which the operator was invoked.

Looking at your code, you are allocating a new Air object:
Code:
Air *current = new Air;
and never using it, since your next line has another assignment:
Code:
current = getpass.head

And you are never deallocating it, which is a poor choice. I am under the strong impression that this will `leak memory'.

As Mike said, it would be really helpful for us to see the definition of an Air object.

the post following this is quite funny. I don't think I needed to add a reply though.
 
And you are never deallocating it, which is a poor choice. I am under the strong impression that this will `leak memory'.
Yes; it'll leak Air. Get it? I said "leak Air". OMFG, I am teh funnay.
 
Back
Top