2017-02-07 41 views
-1

我是新来的类和面向对象编程。我们的老师让我们创建一个程序,该程序必须包含文件.cpp,主文件.cpp和文件.hppC++:在编译时获取多个“多重定义”错误

这里是每个文件:

首先,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 
+4

变化'的#include “odometer.cpp”''到#include“odometer.hpp”' –

+0

同意。无论如何,你为什么补充说? –

+0

我们的老师告诉我们要这样做,甚至在示例文件中包含它。 – CodyT96

回答

0

如果您包括您odometer.cpp到您的odometer_main.cpp,你不能还分别编译你的里程表。 CPP并将其与您的odometer_main.cpp链接。在这种情况下,是的,你会得到重复的符号。但是,典型的方法是只将.hpp文件包含到主文件中,其他所有文件都应该编译并链接正常。

1

总之,用#include "odometer.hpp"代替#include "odometer.cpp"。现在为什么。

您的程序由源文件组成,如main.cppodometer.cpp。这些文件包含项目中函数,变量或类的定义。您的编译器将每个源文件(.cpp)分别编译到目标文件(.o)中,然后链接器将这些目标文件链接在一起以形成程序。

但是,虽然您的源文件是单独编译的,但它们需要互相交互。 main.cpp将要创建Odometer对象并访问Odometer成员函数等。所以我们需要一种方法来告诉main.cpp什么是Odometer。最简单的方法是在头文件中定义Odometer,在main.cpp中定义#include头。

#include预处理指令,它将另一个文件的内容插入到当前文件中。预处理器在代码实际编译之前运行。这个想法是,你在一个文件中有一些声明,我们称之为头文件,以及需要访问这些声明的多个源文件。所以每个源文件的标题将为#include

不过,请注意main.cpp不需要访问定义Odometer的成员函数,只需要知道这些功能,以及如何给他们打电话。该信息在odometer.hpp的类定义中,而不是在odometer.cpp中。这对#include "odometer.cpp"是个错误,因为那时我们会在两个不同的地方定义函数,链接器会发出抱怨。

所以一般你把你的类定义在头文件(.hpp),把类实现的源文件(.cpp),并#include头文件中需要访问该类任何其他的源文件。如果以这种方式正确构建程序,则不应将#include一个.cpp文件转换为另一个.cpp文件。这里

0

的多个问题:

odometer.hpp

  • Odometer(float g, int m):这是不是在odometer.cpp
  • Odometer()定义:这个默认的构造函数也应在odometer.cpp

  • 定义
  • 里程表有三个成员变量,但只有两个是作为输入,目前还不清楚有第三mpg初始化

  • 没有包括守卫在你的头文件

    #ifdef ODO_H

    #define ODO_H

    class Odometer{ // your class declaration };

    #endif

odometer.cpp

  • #include "odometer.hpp"

  • #include <iostream> - 因为你正在使用std::cout

  • 提供各种定义0构造函数。

的main.cpp

  • #include "odometer.hpp" - 因为你使用的是这里

里程表类解决这些问题,应该可以帮助您编译代码。