我得到的错误是:
||=== Build: Debug in Building Sim (compiler: GNU GCC Compiler) ===|
obj/Debug/main.o||In function `__static_initialization_and_destruction_0':|
/home/josh/Documents/cpp/building_sim/main.cpp|15|undefined reference to `User::~User()'|
/home/josh/Documents/cpp/building_sim/main.cpp|20|undefined reference to `User::~User()'|
/home/josh/Documents/cpp/building_sim/main.cpp|20|undefined reference to `Building::~Building()'|
/home/josh/Documents/cpp/building_sim/main.cpp|20|undefined reference to `User::~User()'|
||=== Build failed: 4 error(s), 0 warning(s) (0 minute(s), 0 second(s)) ===|
我已经有几天这个错误了,已经看到了一个好的14-18个线程,这个问题的答案对我来说绝对没有任何帮助。此时我的项目由五个文件组成:
main.cpp
user.hpp
user.cpp
building.hpp
building.cpp
main.cpp链接building.hpp和user.hpp,building和user.cpp链接各自的头文件和building.cpp链接user.hpp。
我尝试过两次不同的方式来构建这个项目。我创建了一个shell脚本来将所有三个cpp文件构建到.o文件然后加入它们(以所有可能的顺序),我在所有可能的命令中尝试了g++ main.cpp building.cpp user.cpp
,我创建了一个Code :: Blocks项目来链接手动将所有文件放在一起。我的所有头文件都包含警卫,但我不认为这是一个问题。
我的文件的全文如下。
的main.cpp
#include <stdio.h>
#include "building.hpp"
#include "user.hpp"
// TODO Text parsing
// TODO Resources
// TODO Generations
using namespace std;
//----------------------------------
// Variables
//----------------------------------
//-----User------
User user(1000);
//---Buildings---
int numBuildings = 0;
Building building1(user, 10, 14, "George");
Building buildings[] = {building1};
int main(){
return 0;
}
user.hpp
#include <stdio.h>
#ifndef user_h
#define user_h
class User{
public:
User();
~User();
User(int _money);
int getMoney();
void setMoney(int _money);
int getBuildings();
void setBuildings(int _buildings);
private:
int money;
int buildings;
};
#endif
user.cpp
#include "user.hpp"
User::User(){
User::money = 1000;
User::buildings = 0;
}
User::User(int _money){
User::money = _money;
setBuildings(getBuildings()+1);
}
int User::getMoney(){
return User::money;
}
void User::setMoney(int _money){
User::money = _money;
}
int User::getBuildings(){
return User::buildings;
}
void User::setBuildings(int _buildings){
User::buildings = _buildings;
}
building.hpp
#include <iostream>
#include <string>
#include "user.hpp"
#ifndef building_h
#define building_h
class Building{
public:
Building();
~Building();
Building(User user);
Building(User user, int _cost, int _payout);
Building(User user, int _cost, int _payout, std::string _name);
int getCost();
int getPayout();
private:
int cost;
int payout;
std::string name;
};
#endif
building.cpp
#include "building.hpp"
Building::Building(){
std::cout << "\n[ERROR] Default constructor used, no building created.\n";
}
Building::Building(User user){
Building::cost = 10;
Building::payout = 20;
Building::name = "Generic";
user.setBuildings(user.getBuildings()+1);
}
Building::Building(User user, int _cost, int _payout){
Building::cost = _cost;
Building::payout = _payout;
Building::name = "Generic";
user.setBuildings(user.getBuildings()+1);
}
Building::Building(User user, int _cost, int _payout, std::string _name){
Building::cost = _cost;
Building::payout = _payout;
Building::name = _name;
user.setBuildings(user.getBuildings()+1);
}
int Building::getCost(){
return Building::cost;
}
int Building::getPayout(){
return Building::payout;
}
答案 0 :(得分:2)
您只是忘了在相应的cpp模块中定义两个析构函数:User和Building。