• Some users have recently had their accounts hijacked. It seems that the now defunct EVGA forums might have compromised your password there and seems many are using the same PW here. We would suggest you UPDATE YOUR PASSWORD and TURN ON 2FA for your account here to further secure it. None of the compromised accounts had 2FA turned on.
    Once you have enabled 2FA, your account will be updated soon to show a badge, letting other members know that you use 2FA to protect your account. This should be beneficial for everyone that uses FSFT.

C++ << operator overloading

Joined
May 19, 2007
Messages
8
I am trying to overload the << operator, and use it to print out a list.
For some reason, it isn't working out.

Here's my class deifnition:
Code:
class JGAirlines
{

	friend ostream &operator<<(ostream &,JGAirlines &);    
      
	public:
		JGAirlines(int,int);  // Constructor
		~JGAirlines(); // Destructor
		void RegisterPassenger(); // Enters a passenger name.
		void RegisterPassenger(string);
		void Delete(); //Deletes a passenger from the flight roster.
		void PrintFlightManifest();
		bool FindPassenger(string); //Finds a passenger on a flight.
		void Together(); //Registers passengers flying together.
		void Merge(); //Merges two flights.
		void Seat(); //Assigns seat numbers to all passengers.
		void ReadFile(); //Reads a set of passengers from a file.
		
		

	private:
            
        struct AirNode // node that represents passenger record
        {
			string person; // passenger's name
			AirNode *next; // pointer to the next passenger record
        };
		
		int flightID; // unique ID of the flight
		int maxSize; // maximum capacity of the flight
		int size; // number of passengers on board
		AirNode *head; // head of the linked list of passengers in alphabetical order
		
};



#endif

		
};



#endif

The function:

Code:
ostream &operator<<(ostream &output, JGAirlines &getPassenger)
{
	for(JGAirlines *current = getPassenger.head; current != NULL; current = current->next)
	{
		output << current->person <<endl; 
	}

	return output; 
}

When I compile I get these messages:
error C2440: 'initializing' : cannot convert from 'JGAirlines::AirNode *' to 'JGAirlines *'

error C2039: 'next' : is not a member of 'JGAirlines'

error C2039: 'person' : is not a member of 'JGAirlines'

I tried using:
Code:
friend ostream &operator<<(ostream &,AirNode&);

Instead of:
Code:
friend ostream &operator<<(ostream &,JGAirlines &);

but it still doesn't work.
Can anyone tell me what I'm doing wrong?
 
The errors have nothing to do with the operator overload. Just as the error messages suggest, I suggest you look at the types in each part of the code that's causing an error.

Here is the code causing each error:

error C2440: 'initializing' : cannot convert from 'JGAirlines::AirNode *' to 'JGAirlines *' is caused by:
Code:
JGAirlines *current = getPassenger.head

error C2039: 'next' : is not a member of 'JGAirlines' is caused by:
Code:
current = current->next

error C2039: 'person' : is not a member of 'JGAirlines' is caused by:
Code:
current->person
 
What bassman said is right on. I think you might've confused yourself by embedding your struct within the scope of your class. Nothing wrong with that, and this is a good time to use such a technique ... but you need to understand the scoping rules and operators if you want to do it right.
 
The errors have nothing to do with the operator overload. Just as the error messages suggest, I suggest you look at the types in each part of the code that's causing an error.

Here is the code causing each error:

error C2440: 'initializing' : cannot convert from 'JGAirlines::AirNode *' to 'JGAirlines *' is caused by:
Code:
JGAirlines *current = getPassenger.head

error C2039: 'next' : is not a member of 'JGAirlines' is caused by:
Code:
current = current->next

error C2039: 'person' : is not a member of 'JGAirlines' is caused by:
Code:
current->person

Yes, I understand what the errors mean. I guess I should have stated the question clearer. What I meant to ask was, can anyone tell me how to fix it?
 
Like Colossusx10 said, look at the types. What types are current and getPassenger.head, and why can't you assign one to the other?
 
Yes, I understand what the errors mean. I guess I should have stated the question clearer. What I meant to ask was, can anyone tell me how to fix it?

I don't think anyone is going to tell you *how* to fix it - but we will give you hints so you learn what the problem is on your own. AdamBomb gave a great hint about the first error.

For the second and third errors, look at exactly which class/struct directly contains next and person. Does the class pointed to by current contain them?
 
Back
Top