Operator Overloading

I am trying to overload some operators, using objects. The program compiles fine, but crashes when run, right after getting to the overloading part. Here is the meat of what I am using:

(in driver)

(cab_9999 + cab_flightNum); //second object is current object


(in header file/implementation file after definitions)

const Cabin operator +( const Cabin& allFlight, const Cabin& thisFlight)


(in functions of implementation file)
const Cabin operator +( const Cabin& allFlight, const Cabin& thisFlight)
{
(print test message)
}

Just doing this crashes it. I eventually need to be able to get one int from thisFlight and add it to an allFlight int, but I can't get this to even work, so I am stuck. Any thoughts on either? I appreciate the help. -Scott
I suspect that what you have posted has nothing to do with the problem, but I have not seen a const return value on + before... It should be:

Cabin operator + ( const Cabin& leftFlight, const Cabin& rightFlight )

If that doesn't make a difference (and I don't think it will), please post more code.

Hope this helps.
Last edited on
Thanks for the idea. I tried that, and it did not change anything. I am not sure how to post the nice version of code most people post, but here is a text version of my driver, minus some simple text to the user.

File Name: Driver_7.cpp
#include <iostream>
#include <iomanip>
#include <string>
#include <cmath>
#include "Cabin.h" // Builds Cabin class

using namespace std; // Namespaces

char exitWord; //Used to exit program if user chooses to
int flightNum; //flightnum variable for the user to input the flight number
int pass_count; // passenger count for the flight.

//Initialize the overall cabin, flight 9999
Cabin cab_9999(9999);

int main()
{
//User Greeting
//Begin do-while loop for adding flights
do
{
//Ask the user for the flight number
cin >> flightNum;
//Create a Cabin object, and pass it the flight number
Cabin cab_flightNum(flightNum);
// initialize the pass_count
pass_count = 1;
//Increment the number of flights(STATIC FUNCTION)
Cabin::incFlights();
//Begin Do-While for each Cabin
do
{
cab_flightNum.showCabin();
cab_flightNum.reserveSeat(pass_count);
// Increment the number of passengers for the flight
pass_count++;
//Show the cabin again
cab_flightNum.showCabin();
//Show the number of passengers on the plane
cab_flightNum.getNumPass();
//Ask the user if they are done adding passengers to the flight
cin >> exitWord;
//End do-While loop for Cabin/flight
}while (exitWord != 'n');
//Now calculate and display forward and aft cabin weight
cab_flightNum.getForAftWt();

//Now add passengers from this flight to the master flight.
(cab_9999 + cab_flightNum);

//Ask user if they are done with program
cin >> exitWord;
cout << "\n";
if ( exitWord == 'y')
{
cout << "There were " << cab_flightNum.showFlights()
<< " flights scheduled today.\n\n";
}
//End do-While loop for program
} while ( exitWord != 'y');

system("PAUSE"); // Keeps windows screen up
return 0;
}

Here is the .h/implementation file (all in one) with only the initialization, and overloaded function.

File Name: Cabin.h
#include <iostream> // Preprocessor Directives
#include <iomanip>
#include <cmath> // Math functions

using namespace std; // Namespaces

const int rnum = 12; //Represents the number of rows in the aircraft
const int snum = 3; //Represents the number of seats per row
const int max_Weight = 6120; //Maximum Aircraft weight limit for passengers

//Class construction
class Cabin
{
public:
Cabin(int); //Constructor (User adds the flight num)
Cabin(); //Default Constructor
int cabArray[rnum][snum]; //Array for the cabin(Initialized here)
string dLocation; //String for departure location
string aLocation; //String for arrival location
void fillArray(); //Fills/Initilizes array
void showCabin(); //Prints array/cabin view to screen
void reserveSeat(int); //Fill individual array block/seat
void getACWeight(); //Gets total Aircraft passenger weight
void getForAftWt(); //Finds the weight of the forward and aft
//sections of the plane for balance.
void swapSeats(); //Allow user to swap pass data from seat
//to seat
void getNumPass(); //Gets number of passengers
int numPass; //Number of passengers

//Static integer to count the number of flights (Cabins created)
static int flt_Count;
//Static function to increment the number of flights
static int incFlights(){flt_Count++;}
//Member funtion to show the number of flights
int showFlights(){return flt_Count;}

private:
int flightNumber; //Holds the flight number
};

//const Cabin operator +(const Cabin& allFlight, const Cabin& thisFlight);
Cabin operator +(const Cabin& allFlight, const Cabin& thisFlight);

Cabin::Cabin(int flightNum) //Class Constructor
{
//initialize the flightNumber variable
flightNumber = flightNum;

//Fill/initilize the array (Runs the fillArray function)
fillArray();
}

//************************************************************************
void Cabin::getNumPass()
{
int fnum; //Counter for the row loop
int fsnum; //Counter for the seat loop
int numPass=0; //Number of passengers

//Loop through the array
for (int fnum = 0; fnum < rnum; fnum++)
{
for (fsnum = 0; fsnum < snum; fsnum++)
{
//Check for weight, and add a passenger
if (cabArray[fnum][fsnum] > 0)
{
numPass++;
}
}
}
//Print the number of passengers
cout << "\nThere are " << numPass << " passengers now on flight "
<< flightNumber << "\n\n";
}


//************************************************************************
//const Cabin operator +( const Cabin& allFlight, const Cabin& thisFlight)
Cabin operator +( const Cabin& allFlight, const Cabin& thisFlight)

{
//int my_flight;
//my_flight = (allFlight.numPass + thisFlight.numPass);
//allFlight.numPass = my_flight;

cout << "Test message";

// cout << " I got " << my_flight << " total passengers...\n";
//allFlight.numPass = (allFlight.numPass + thisFlight.numPass);
}

I think that should be the relevant code. Again, sorry for the length. Thanks for the help!
Argh! Code tags please!
Code tags?
[code]
#include <iostream>

int main(){
std::cout <<"Hello, World!"<<std::endl;
return 0;
}
[/code]
becomes
1
2
3
4
5
6
#include <iostream>

int main(){
    std::cout <<"Hello, World!"<<std::endl;
    return 0;
}
Last edited on
Ahhhh, much better, thanks.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
File Name: Driver_7.cpp 
#include <iostream>      
#include <iomanip>
#include <string>
#include <cmath>                        
#include "Cabin.h"                           // Builds Cabin class

using namespace std;                         // Namespaces

char exitWord;     //Used to exit program if user chooses to
int flightNum;     //flightnum variable for the user to input the flight number
int pass_count;    // passenger count for the flight.

//Initialize the overall cabin, flight 9999
Cabin cab_9999(9999);

int main()
    {
    //User Greeting        
    //Begin do-while loop for adding flights     
    do
    {
        //Ask the user for the flight number
        cin >> flightNum;           
        //Create a Cabin object, and pass it the flight number
        Cabin cab_flightNum(flightNum);        
        // initialize the pass_count
        pass_count = 1;
         //Increment the number of flights(STATIC FUNCTION)
        Cabin::incFlights();        
        //Begin Do-While for each Cabin
        do
        {
            cab_flightNum.showCabin();
            cab_flightNum.reserveSeat(pass_count);
            // Increment the number of passengers for the flight
            pass_count++;
            //Show the cabin again
            cab_flightNum.showCabin();
            //Show the number of passengers on the plane
            cab_flightNum.getNumPass();
            //Ask the user if they are done adding passengers to the flight    
            cin >> exitWord;
            //End do-While loop for Cabin/flight
        }while (exitWord != 'n');  
    //Now calculate and display forward and aft cabin weight
    cab_flightNum.getForAftWt(); 

    //Now add passengers from this flight to the master flight.  
    (cab_9999 + cab_flightNum);

    //Ask user if they are done with program
    cin >> exitWord;
    cout << "\n";
    if ( exitWord == 'y')
        {
            cout << "There were " << cab_flightNum.showFlights()
                 << " flights scheduled today.\n\n";
         }     
    //End do-While loop for program    
    } while ( exitWord != 'y');

    system("PAUSE");                         // Keeps windows screen up
	return 0;
    }

Here is the .h/implementation file (all in one) with only the initialization, and overloaded function.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
File Name: Cabin.h
#include <iostream>                          // Preprocessor Directives
#include <iomanip>
#include <cmath>                             // Math functions

using namespace std;                         // Namespaces

const int rnum = 12;          //Represents the number of rows in the aircraft
const int snum = 3;           //Represents the number of seats per row
const int max_Weight = 6120;  //Maximum Aircraft weight limit for passengers

//Class construction
class Cabin 
{
    public:
        Cabin(int);                 //Constructor (User adds the flight num)
        Cabin();                    //Default Constructor
        int cabArray[rnum][snum];   //Array for the cabin(Initialized here)
        string dLocation;           //String for departure location
        string aLocation;           //String for arrival location
        void fillArray();           //Fills/Initilizes array
        void showCabin();           //Prints array/cabin view to screen
        void reserveSeat(int);      //Fill individual array block/seat
        void getACWeight();         //Gets total Aircraft passenger weight
        void getForAftWt();         //Finds the weight of the forward and aft
                                    //sections of the plane for balance.
        void swapSeats();           //Allow user to swap pass data from seat
                                    //to seat
        void getNumPass();          //Gets number of passengers
        int numPass;                //Number of passengers
        
        //Static integer to count the number of flights (Cabins created)
        static int flt_Count;        
        //Static function to increment the number of flights
        static int incFlights(){flt_Count++;}
        //Member funtion to show the number of flights
        int showFlights(){return flt_Count;}
        
    private:
        int flightNumber;   //Holds the flight number
};
        
        //const Cabin operator +(const Cabin& allFlight, const Cabin& thisFlight);
        Cabin operator +(const Cabin& allFlight, const Cabin& thisFlight);
        
Cabin::Cabin(int flightNum)     //Class Constructor
{   
    //initialize the flightNumber variable
    flightNumber = flightNum;

    //Fill/initilize the array (Runs the fillArray function)
    fillArray();
}
    
//************************************************************************
void Cabin::getNumPass()
    {
        int fnum;               //Counter for the row loop
        int fsnum;              //Counter for the seat loop
        int numPass=0;          //Number of passengers
        
        //Loop through the array
        for (int fnum = 0; fnum < rnum; fnum++)
            {
                for (fsnum = 0; fsnum < snum; fsnum++)
                {
                    //Check for weight, and add a passenger
                    if (cabArray[fnum][fsnum] > 0)
                        {
                            numPass++;
                        }
                }
            }
            //Print the number of passengers
            cout << "\nThere are " << numPass << " passengers now on flight "
                 << flightNumber << "\n\n";
     }


//************************************************************************
//const Cabin operator +( const Cabin& allFlight, const Cabin& thisFlight)
Cabin operator +( const Cabin& allFlight, const Cabin& thisFlight)

    {
        //int my_flight;
        //my_flight = (allFlight.numPass + thisFlight.numPass);
        //allFlight.numPass = my_flight;

        cout << "Test message";

       // cout << " I got " << my_flight << " total passengers...\n";        
        //allFlight.numPass = (allFlight.numPass + thisFlight.numPass);
     }

I think that should be the relevant code. Again, sorry for the length. Thanks for the help!
Topic archived. No new replies allowed.