gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 00:50:46 24 4
gpt4 key购买 nike

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

提前致谢!

狗.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

};

狗.cpp:

#include "Dog.hpp"
using namespace std;


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

int Dog::getAge() const
{
return itsAge;
}

** main.cpp

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

int main()
{
Dog aDog(5);
cout << aDog.getAge();
return 0;
}

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)

最佳答案

您似乎在具有函数 main 的模块中包含了带有成员函数定义的 cpp 模块 (Dog.cpp)。您应该只包含标题。

按以下方式修改Main.cpp

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

int main()
{
Dog aDog(5);
cout << aDog.getAge();
return 0;
}

考虑到您忘记在构造函数中设置数据成员 itsAge。它应该看起来像

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

而且您还忘记了定义析构函数。

你可以写在类定义中,例如下面的方式

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

};

前提是编译器支持这个说明符。

关于c++ - C++中的多重定义错误,使用头文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23859623/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com