c++ - 在函数未定义参考 c++
问题描述
我一直收到一个奇怪的错误。
/usr/lib/gcc/x86_64-linux-gnu/5/../../../x86_64-linux-gnu/crt1.o:
在函数_start: >(.text+0x20): undefined reference to main /tmp/cc4ZqKzy.o:
在函数 `Sep::Building::Building(Sep::Field::FieldType, >std::__cxx11::basic_string, >std::allocator >, char, bool, bool, unsigned int, unsigned int) 中:
Building.cpp:(.text+0x3c): 未定义对 Sep::Field::Field() 的引用 collect2:
错误:ld 返回 1 个退出状态
我读了很多关于这个问题的书,但没有一个有相同的。我包括了所有的标题,还添加了 ifndef 保护。
主.cpp:
#include "Field.h"
#include "Building.h"
namespace Sep
{
just some returns...
}
int main(int argc, char *argv[])
{
Sep::Building Haus(Sep::Field::FieldType::HOME,"HOME", 'H', true, true, 100, 100);
std::cout << "HAUS ABREV:" << Haus.getAbbrevationOnField() << '\n';
}
字段.h
#include <cstring>
#include <string>
#include <iostream>
#include <memory>
#ifndef FIELD_H
#define FIELD_H
namespace Sep
{
//----------------------------------------------------------------------------
// Field class, containing all needed information to create a Field object
//
class Field
{
public :
enum FieldType \
{GRASS, WATER, OBSTACLE, STREET, HOME, MARKET, CLINIC, TOWNHALL};
private:
FieldType type_;
std::string name_;
char abbrevation_;
bool buildable_;
bool destroyable_;
unsigned int build_cost_;
unsigned int destroy_cost_;
public:
//------------------------------------------------------------------------
// Field constructors & destructor
//
Field();
Field(FieldType type);
~Field() noexcept;
//------------------------------------------------------------------------
//getters
//
Field::FieldType getFieldType() const { return type_; };
const char getAbbrevationOnField() const { return abbrevation_; };
//------------------------------------------------------------------------
//setters
//
static std::string getName(FieldType type);
FieldType getType() const;//steht oben in getFiel3dType Z55
void setType(FieldType type){type_ = type;};
void setName(std::string name){name_ = name;};
void setAbbrevation(char abbrev){abbrevation_ = abbrev;};
void setBuildable(bool buildable){buildable_ = buildable;};
void setDestroyable(bool destroyable){destroyable_ = destroyable;};
void setBuildCost(int b_cost){build_cost_ = b_cost;};
void setDestroyCost(int d_cost){destroy_cost_ = d_cost;};
};
}
#endif //FIELD.H
字段.cpp
#include "Field.h"
#include <cstring>
#include <string>
#include <iostream>
#include <memory>
using Sep::Field;
//------------------------------------------------------------------------------
// Setter of the private FieldType type_ to the given param
//
// @param the type of field to get set
//
Field::Field(FieldType type)
{
type_ = type;
};
Field::~Field(){};
//------------------------------------------------------------------------------
// Checks the type of a given field, returns the name of type as string
//
// @param type, the type of the field to check
//
// @return string the name of the type of the checked field
//
std::string Field::getName(FieldType type)
{
switch (type)
{
case GRASS:
return std::string("Grass");
case WATER:
return std::string("Water");
case OBSTACLE:
return std::string("Obstacle");
case STREET:
return std::string("Street");
case HOME:
return std::string("Home");
case MARKET:
return std::string("Market");
case CLINIC:
return std::string("Clinic");
case TOWNHALL:
return std::string("Town Hall");
default:
return std::string("Unknown Field");
}
};
//------------------------------------------------------------------------------
// getters
//
// Getter from the private FieldType type_
//
// @param none
//
// @return the type of type_ as FieldType
//
Field::FieldType Field::getType() const
{
return type_;
};
建筑.h
#ifndef BUILDING_H
#define BUILDING_H
#include "Field.h"
namespace Sep
{
class Building : public Field
{
private:
public:
Building(FieldType type, const std::string name, const char abbrevation, \
const bool buildable, const bool destroyable,\
const unsigned int b_cost, const unsigned int d_cost);
~Building();
};
}
#endif //BUILDING_H
构建.cpp
#include "Building.h"
#include "Field.h"
Sep::Building::Building(FieldType type, const std::string name, \
const char abbrevation, \
const bool buildable, const bool destroyable,\
const unsigned int b_cost, const unsigned int d_cost)
{
Sep::Field::setType(type);
Sep::Field::setName(name);
Sep::Field::setAbbrevation(abbrevation);
Sep::Field::setBuildable(buildable);
Sep::Field::setDestroyable(destroyable);
Sep::Field::setBuildCost(b_cost);
Sep::Field::setDestroyCost(d_cost);
};
Sep::Building::~Building(){};
有人有想法吗?因为我经常在这个项目中但在其他类中遇到这个错误。奇怪的是,程序似乎可以正确编译,但在开始时我得到了这个 collect2: error: ld returned 1 exit status。
谢谢
解决方案
Field.cpp 需要更改,如果不想使用 Field()构造函数,只需将 Field() 构造函数的定义置空即可。
例如: Field.cpp
#include "Field.h"
#include <cstring>
#include <string>
#include <iostream>
#include <memory>
using Sep::Field;
//------------------------------------------------------------------------------
// Setter of the private FieldType type_ to the given param
//
// @param the type of field to get set
//
Field::Field(){
//empty constructor or can initialize type_ to default value.
}
Field::Field(FieldType type)
{
type_ = type;
};
Field::~Field(){};
//------------------------------------------------------------------------------
// Checks the type of a given field, returns the name of type as string
//
// @param type, the type of the field to check
//
// @return string the name of the type of the checked field
//
std::string Field::getName(FieldType type)
{
switch (type)
{
case GRASS:
return std::string("Grass");
case WATER:
return std::string("Water");
case OBSTACLE:
return std::string("Obstacle");
case STREET:
return std::string("Street");
case HOME:
return std::string("Home");
case MARKET:
return std::string("Market");
case CLINIC:
return std::string("Clinic");
case TOWNHALL:
return std::string("Town Hall");
default:
return std::string("Unknown Field");
}
};
//------------------------------------------------------------------------------
// getters
//
// Getter from the private FieldType type_
//
// @param none
//
// @return the type of type_ as FieldType
//
Field::FieldType Field::getType() const
{
return type_;
};
推荐阅读
- python - “ValueError:具有多个元素的数组的真值不明确。” 使用 scipy.integrate.dblquad 时
- python - 未找到夹具 is_udp(尽管之前已声明了几行)
- swift - 将 RGB 颜色快速更改为 HSV
- laravel-5 - 在 Eloquent 访问器中使用 Eloquent 关系?
- python - ValueError:无法将字符串转换为浮点数:读取 .csv 数据集时
- quorum - 如何获取事务管理器的所有可用公钥列表(用于 privateFor 字段)
- angular - Zoho Subscription 与 Angular 5 的集成
- php - 尽管允许所有流量,PHP-cURL 出站调用在 localhost 中有效,但在 AWS 托管网站中无效
- html - 使用页面滚动更改标题背景的问题
- android - 第一个活动的 onPause 不被调用时连续启动两个活动和具有半透明主题的第二个活动