我是新來的類和麪向對象編程。我們的老師讓我們創建一個程序,該程序必須包含文件.cpp
,主文件.cpp
和文件.hpp
。C++:在編譯時獲取多個「多重定義」錯誤
這裏是每個文件:
首先,odometer.hpp
文件:
class Odometer
{
int miles;
float gallons, mpg;
public:
//Constructors
Odometer(); //Default
Odometer(float g, int m);
//Mutator Functions
void Set_miles(int m);
void Set_gallons(float g);
//Functions
void Add_trip(int m, float g);
int Check_mileage(float g);
void Print_info();
//Accessor Functions
float Get_mpg();
float Get_gallons();
int Get_miles();
};
接下來,odometer.cpp
文件:
#include "odometer.hpp"
#include <iostream>
//Constructors
Odometer::Odometer()
{
miles = 0;
gallons = 0.0;
mpg = 0.0;
}
Odometer::Odometer(float g, int m)
{
miles = m;
gallons = g;
mpg = m/g;
}
//Mutator functions
void Odometer::Set_miles(int m)
{
miles = m;
}
void Odometer::Set_gallons(float g)
{
gallons = float(g);
}
//Accessor functions
float Odometer::Get_mpg()
{
return mpg;
}
float Odometer::Get_gallons()
{
return gallons;
}
int Odometer::Get_miles()
{
return miles;
}
//Other functions
//Takes # of gallons & # of miles and adds it to previous values, calculating
//new miles/gallon for whole trip
void Odometer::Add_trip(int m, float g)
{
miles += m;
gallons += g;
mpg = miles/gallons;
}
int Odometer::Check_mileage(float g)
{
int newMiles = g * mpg;
return newMiles;
}
void Odometer::Print_info()
{
std::cout << "Miles: " << miles << " Gallons: " << gallons <<
" Miles/Gallon: " << mpg;
}
最後,odometer_main.cpp
文件(到目前爲止,它不完整):
#include <iostream>
#include "odometer.cpp"
using namespace std;
int main(void)
{
//Odometer odDefault; //Odometer object set to defaults
Odometer od(10, 100); //Odometer object with values set
return 0;
}
這是當我嘗試編譯所有的文件,我發現了錯誤:
/tmp/ccArjYHP.o: In function 'Odometer::Odometer()': odometer_main.cpp:(.text+0x0): multiple definition of 'Odometer::Odometer()' /tmp/cc1W9Ght.o:odometer.cpp:(.text+0x0): first defined here /tmp/ccArjYHP.o: In function 'Odometer::Odometer()': odometer_main.cpp:(.text+0x0): multiple definition of 'Odometer::Odometer()' /tmp/cc1W9Ght.o:odometer.cpp:(.text+0x0): first defined here /tmp/ccArjYHP.o: In function 'Odometer::Odometer(float, int)': odometer_main.cpp:(.text+0x30): multiple definition of 'Odometer::Odometer(float, int)' /tmp/cc1W9Ght.o:odometer.cpp:(.text+0x30): first defined here /tmp/ccArjYHP.o: In function 'Odometer::Odometer(float, int)': odometer_main.cpp:(.text+0x30): multiple definition of 'Odometer::Odometer(float, int)' /tmp/cc1W9Ght.o:odometer.cpp:(.text+0x30): first defined here /tmp/ccArjYHP.o: In function 'Odometer::Set_miles(int)': odometer_main.cpp:(.text+0x72): multiple definition of 'Odometer::Set_miles(int)' /tmp/cc1W9Ght.o:odometer.cpp:(.text+0x72): first defined here /tmp/ccArjYHP.o: In function 'Odometer::Set_gallons(float)': odometer_main.cpp:(.text+0x8a): multiple definition of 'Odometer::Set_gallons(float)' /tmp/cc1W9Ght.o:odometer.cpp:(.text+0x8a): first defined here /tmp/ccArjYHP.o: In function 'Odometer::Get_mpg()': odometer_main.cpp:(.text+0xa8): multiple definition of 'Odometer::Get_mpg()' /tmp/cc1W9Ght.o:odometer.cpp:(.text+0xa8): first defined here /tmp/ccArjYHP.o: In function 'Odometer::Get_gallons()': odometer_main.cpp:(.text+0xbc): multiple definition of 'Odometer::Get_gallons()' /tmp/cc1W9Ght.o:odometer.cpp:(.text+0xbc): first defined here /tmp/ccArjYHP.o: In function 'Odometer::Get_miles()': odometer_main.cpp:(.text+0xd0): multiple definition of 'Odometer::Get_miles()' /tmp/cc1W9Ght.o:odometer.cpp:(.text+0xd0): first defined here /tmp/ccArjYHP.o: In function 'Odometer::Add_trip(int, float)': odometer_main.cpp:(.text+0xe0): multiple definition of 'Odometer::Add_trip(int, float)' /tmp/cc1W9Ght.o:odometer.cpp:(.text+0xe0): first defined here /tmp/ccArjYHP.o: In function 'Odometer::Check_mileage(float)': odometer_main.cpp:(.text+0x140): multiple definition of 'Odometer::Check_mileage(float)' /tmp/cc1W9Ght.o:odometer.cpp:(.text+0x140): first defined here /tmp/ccArjYHP.o: In function 'Odometer::Print_info()': odometer_main.cpp:(.text+0x168): multiple definition of 'Odometer::Print_info()' /tmp/cc1W9Ght.o:odometer.cpp:(.text+0x168): first defined here collect2: error: ld returned 1 exit status makefile:2: recipe for target 'odometer' failed make: *** [odometer] Error 1
變化'的#include 「odometer.cpp」''到#include「odometer.hpp」' –
同意。無論如何,你爲什麼補充說? –
我們的老師告訴我們要這樣做,甚至在示例文件中包含它。 – CodyT96