我对用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
};Dog.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)发布于 2014-05-25 20:29:22
您似乎将cpp模块(Dog.cpp)包含在函数main模块中的成员函数定义中。您应该只包含标头。
按以下方式更改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
};只要编译器支持此说明符。
发布于 2014-05-25 20:30:37
试着在“Dog.hpp”中这样做:
#ifndef HEADER_GUARD // Top of file
#define HEADER_GUARD
/* Code in your class here. */
#endif // End of file或者文件顶部的这个:
#pragma once前一个代码块将告诉预处理器只在没有定义头保护定义(例如“FILENAME_HPP”)的情况下将代码包含在保护程序中。如果条件传递,它将立即被定义,因此代码应该只加载一次。
后一个块也将在大多数编译器(包括g++和VisualC++)上工作--而且可能更有效。它告诉编译器只处理一次文件。
此外,您似乎缺少了类的析构函数的定义。
另一个编辑:还有一件事,对#include .cpp文件的编程实践非常糟糕。始终对与其关联的头文件进行#include。
https://stackoverflow.com/questions/23859623
复制相似问题