C++中的多重定义错误,使用头文件

Dav*_*504 1 c++ linker-errors undefined-reference

我刚接触C++编程,但有一些Java经验.我创建了一个非常基本的类Dog.cpp及其头文件Dog.hpp.Netbeans不会构建项目,给我一个错误,说明构造函数和getAge函数的多个定义.就我而言,我已经在头文件中声明了构造函数和函数,并在类文件中定义它们.我哪里出错了?

提前致谢!

Dog.hpp:

#include <iostream>
using namespace std;
class Dog 
{
public:
    Dog(int someAge);           // Constructor
    ~Dog();                     // Destructor
    int getAge() const;         // function prototype
private:
    int itsAge;                 // age variable

};
Run Code Online (Sandbox Code Playgroud)

Dog.cpp:

#include "Dog.hpp"
using namespace std;


Dog::Dog(int anAge) 
{
    cout << "Dog created \n";
}

int Dog::getAge() const
{
    return itsAge;
}
Run Code Online (Sandbox Code Playgroud)

**main.cpp

#include <iostream>
#include "Dog.cpp"

int main() 
{
    Dog aDog(5);
    cout << aDog.getAge();
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

Netbeans输出:

"/usr/bin/make" -f nbproject/Makefile-Debug.mk QMAKE= SUBPROJECTS= .build-conf
make[1]: Entering directory `/home/david/NetBeansProjects/C++/SAMS - Hour 10'
"/usr/bin/make"  -f nbproject/Makefile-Debug.mk dist/Debug/GNU-Linux-x86/sams_-_hour_10
make[2]: Entering directory `/home/david/NetBeansProjects/C++/SAMS - Hour 10'
mkdir -p build/Debug/GNU-Linux-x86
rm -f build/Debug/GNU-Linux-x86/main.o.d
g++    -c -g -MMD -MP -MF build/Debug/GNU-Linux-x86/main.o.d -o build/Debug/GNU-Linux-x86/main.o main.cpp
mkdir -p build/Debug/GNU-Linux-x86
rm -f build/Debug/GNU-Linux-x86/Dog.o.d
g++    -c -g -MMD -MP -MF build/Debug/GNU-Linux-x86/Dog.o.d -o build/Debug/GNU-Linux-x86/Dog.o Dog.cpp
mkdir -p dist/Debug/GNU-Linux-x86
g++     -o dist/Debug/GNU-Linux-x86/sams_-_hour_10 build/Debug/GNU-Linux-x86/main.o build/Debug/GNU-Linux-x86/Dog.o  
build/Debug/GNU-Linux-x86/Dog.o: In function `Dog::Dog(int)':
/home/david/NetBeansProjects/C++/SAMS - Hour 10/Dog.cpp:12: multiple definition of `Dog::Dog(int)'
build/Debug/GNU-Linux-x86/main.o:/home/david/NetBeansProjects/C++/SAMS - Hour 10/Dog.cpp:12: first defined here
build/Debug/GNU-Linux-x86/Dog.o: In function `Dog::Dog(int)':
/home/david/NetBeansProjects/C++/SAMS - Hour 10/Dog.cpp:12: multiple definition of `Dog::Dog(int)'
build/Debug/GNU-Linux-x86/main.o:/home/david/NetBeansProjects/C++/SAMS - Hour 10/Dog.cpp:12: first defined here
build/Debug/GNU-Linux-x86/Dog.o: In function `Dog::getAge() const':
/home/david/NetBeansProjects/C++/SAMS - Hour 10/Dog.cpp:18: multiple definition of `Dog::getAge() const'
build/Debug/GNU-Linux-x86/main.o:/home/david/NetBeansProjects/C++/SAMS - Hour 10/Dog.cpp:18: first defined here
build/Debug/GNU-Linux-x86/main.o: In function `main':
main.cpp:(.text+0x6d): undefined reference to `Dog::~Dog()'
main.cpp:(.text+0x7f): undefined reference to `Dog::~Dog()'
collect2: error: ld returned 1 exit status
make[2]: *** [dist/Debug/GNU-Linux-x86/sams_-_hour_10] Error 1
make[2]: Leaving directory `/home/david/NetBeansProjects/C++/SAMS - Hour 10'
make[1]: *** [.build-conf] Error 2
make[1]: Leaving directory `/home/david/NetBeansProjects/C++/SAMS - Hour 10'
make: *** [.build-impl] Error 2

BUILD FAILED (exit value 2, total time: 931ms)
Run Code Online (Sandbox Code Playgroud)

Vla*_*cow 5

看来你在函数main的模块中包含了cpp模块(Dog.cpp)和成员函数定义.您应该只包含标题.

通过以下方式更改Main.cpp

#include <iostream>
#include "Dog.hpp"

int main() 
{
    Dog aDog(5);
    cout << aDog.getAge();
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

考虑到您忘记在construtor中设置数据成员itsAge.它应该看起来像

Dog::Dog(int anAge) : itsAge( anAge )  
{
    cout << "Dog created \n";
}
Run Code Online (Sandbox Code Playgroud)

你也忘了定义析构函数.

您可以在类定义中编写,例如以下方式

class Dog 
{
public:
    Dog(int someAge);           // Constructor
    ~Dog() = default;           // Destructor
    int getAge() const;         // function prototype
private:
    int itsAge;                 // age variable

};
Run Code Online (Sandbox Code Playgroud)

只要编译器支持此说明符.