Hard time with Multiple definitions

My first time compiling as a project, and I am getting multiple definitions errors. I get 2 of them when I try to build the .exe file. I am using 3 files to build with, one is the driver, one header and implementation file.

I figure there is something wrong with this header file, but I am not sure what. I tried using #ifndef for the consts, and even for the whole thing, but that did nothing. I think it might be something in the first 3 consts, but I am not sure.

If the other 2 files are needed, I can add those, but they are pretty long.

Any ideas? Thanks,Scott


// File Name: Cabin.h *

#include <iostream> // Preprocessor Directives
#include <iomanip>
#include <cmath> // Math functions

using namespace std; // Namespaces

const int rnum = 12;
const int snum = 3;
const int max_Weight = 6120;

//Class construction
class Cabin
{
public:
Cabin(int);
Cabin();
int cabArray[rnum][snum];
string dLocation;
string aLocation;
void fillArray();
void showCabin();
void reserveSeat(int);
void getACWeight();
void getForAftWt();
void swapSeats();
void getNumPass();
int numPass;

static int flt_Count;
//Static function to increment the number of flights
//### ONE LINE FUNCTION ALL DONE HERE ####
static int incFlights(){flt_Count++;}

//Member funtion to show the number of flights
//### ONE LINE FUNCTION ALL DONE HERE ####
int showFlights(){return flt_Count;}

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

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

//Fill/initilize the array (Runs the fillArray function)
fillArray();
}
Is any of the methods being defined in a header or file that is included by more than one .cpp? Where is Cabin::flt_Count being defined?

Whenever you're asking about an error, post the error message. Otherwise all we can (usually) do is guess. Particularly since we don't have full access to the code.
Last edited on
Thanks for the response. I set up the flt_Count in the driver as:

//Initializing the static variable for the flt_Count to zero

int Cabin::flt_Count = 0;

I did it this way because I had to have a static variable that incremented upon creation of the Cabin class. It is incremented like this:

Cabin::incFlights();

This cabin.h file is the only shared file, and there is no where else in the other 2 files that have that same items being defined.

As for the error, this is the compile log:

Compiler: Default compiler
Building Makefile: "C:\Documents and Settings\Desktop\Makefile.win"
Executing make clean
rm -f F:/C++_CIS_173/Assign_8/Assign_8.o F:/C++_CIS_173/Assign_8/cabin.o AVCabin.exe

g++.exe -c F:/C++_CIS_173/Assign_8/Assign_8.cpp -o F:/C++_CIS_173/Assign_8/Assign_8.o -I"C:/Dev-Cpp/lib/gcc/mingw32/3.4.2/include" -I"C:/Dev-Cpp/include/c++/3.4.2/backward" -I"C:/Dev-Cpp/include/c++/3.4.2/mingw32" -I"C:/Dev-Cpp/include/c++/3.4.2" -I"C:/Dev-Cpp/include"

g++.exe -c F:/C++_CIS_173/Assign_8/cabin.cpp -o F:/C++_CIS_173/Assign_8/cabin.o -I"C:/Dev-Cpp/lib/gcc/mingw32/3.4.2/include" -I"C:/Dev-Cpp/include/c++/3.4.2/backward" -I"C:/Dev-Cpp/include/c++/3.4.2/mingw32" -I"C:/Dev-Cpp/include/c++/3.4.2" -I"C:/Dev-Cpp/include"

g++.exe F:/C++_CIS_173/Assign_8/Assign_8.o F:/C++_CIS_173/Assign_8/cabin.o -o "AVCabin.exe" -L"C:/Dev-Cpp/lib"

F:/C++_CIS_173/Assign_8/cabin.o(.text+0x100):cabin.cpp: multiple definition of `Cabin::Cabin(int)'
F:/C++_CIS_173/Assign_8/Assign_8.o(.text+0x100):Assign_8.cpp: first defined here
F:/C++_CIS_173/Assign_8/cabin.o(.text+0x308):cabin.cpp: multiple definition of `Cabin::Cabin(int)'
F:/C++_CIS_173/Assign_8/Assign_8.o(.text+0x308):Assign_8.cpp: first defined here
collect2: ld returned 1 exit status

make.exe: *** [AVCabin.exe] Error 1

Execution terminated

Driver file:


#include <iostream>
#include <iomanip>
#include <string>
#include <cmath>
#include "Cabin.h"


using namespace std;

char exitWord; //Used to exit
int flightNum;
int pass_count; // passenger count for the flight.

//Init the static variable for the flt_Count to zero

int Cabin::flt_Count = 0;

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

//Begin Main
int main()
{
//Begin do-while loop for adding flights
do
{
//Ask the user for the flight num
cin >> flightNum;

//Create a Cabin object
Cabin cab_flightNum(flightNum);

//Increment the number of flights
//that have been created(STATIC FUNCTION)
Cabin::incFlights();

//Begin Do-While for each Cabin
do
{
cab_flightNum.showCabin();
cab_flightNum.reserveSeat(pass_count);
cab_flightNum.showCabin();
cab_flightNum.getACWeight();
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 cabin weight
cab_flightNum.getForAftWt();
//Ask user if they are done with program
cin >> exitWord;
//Check to see if user is quitting. If so, print total
//flights for the day.(MEMBER FUNCTION)
if ( exitWord == 'y')
{
cout << "There were " << cab_flightNum.showFlights()
<< " flights scheduled today.\n\n";
}
//End do-While loop for program
} while ( exitWord != 'y');
return 0;
}

If the implementation file is needed, I can add that as well, but it is pretty large.

Thanks,

Scott
Yeah, this definitely looks like you put the definition of Cabin::Cabin() in the header.
I see now. I should put just the generic class definition in the header, and the overloaded ones in the implementation file. Thanks for all of the help!
Topic archived. No new replies allowed.