在函数中未定义的引用 c++
In function undefined reference c++
我总是收到一个奇怪的错误。
/usr/lib/gcc/x86_64-linux-gnu/5/../../../x86_64-linux-gnu/crt1.o:
在函数_start中:>(.text+0x20(:对主的未定义引用/tmp/cc4ZqKzy.o:
在函数 'Sep::Building::Building(Sep::Field::FieldType,>std::__cxx11::basic_string,>std::分配器>, char, bool, bool, unsigned int, unsigned int(:
Building.cpp:(.text+0x3c(:未定义对Sep::Field::Field((的引用 收集2:
错误: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:错误:ld 返回了 1 个退出状态。
感谢
Field.cpp需要更改,如果不想使用 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_;
};
当你尝试构造一个Building
时,Building::Building(...)
构造函数隐式调用其基类构造函数Field::Field()
(因为你没有指定你想要哪个Field
构造函数(。你在Field.h
中承诺这样的构造函数存在于某个地方(这就是为什么编译器从不抱怨(,但你从来没有定义过它。然后,当链接器尝试将声明的函数与编译器发出的函数链接时,它会注意到此构造函数丢失并抱怨。
这就是错误消息试图告诉您的内容:
-
undefined reference to 'Sep::Field::Field()'
->Field::Field()
构造函数未在任何地方定义。 -
In function Sep::Building::Building(...)
-> 它正在尝试调用所示Building
构造函数中的Field
构造函数。
最简单的解决方法是编写Field() = default;
以便编译器自动生成默认构造函数。
编辑:如果你想使用Field::Field(FieldType)
构造函数,这是你将如何做到这一点:
Building::Building(FieldType fieldType, /* etc */)
: Field(fieldType)
{
// etc.
}
您还可以向Field
类添加一个构造函数,该构造函数采用您尝试传递的所有这些参数:
Field::Field(FieldType fieldType, std::string name, char abbrevation, /* etc. */)
: type_(fieldType), name_(name), abbrevation_(abbreviation), /* etc. */
{
}
因此:
Building::Building(FieldType type, const std::string name, const char abbrevation, /* etc. */)
: Field(type, name, abbreviation, /* etc. */)
{
}
更好的是,您可以"重用"长Field
构造函数以进行Building
class Building : public Field
{
public:
using Field::Field;
// ...
}
- 将对象数组的引用传递给函数
- 什么时候在C++中返回常量引用是个好主意
- 我想将一个对T类型的非常量左值引用绑定到一个T类型的临时值
- 何时在引用或唯一指针上使用移动语义
- 如何在c++中使用引用实现类似python的行为
- 编译C++时未定义的引用
- Ctypes wstring通过引用传递
- c++r值引用应用于函数指针
- 理解c++中的引用
- C++取消引用指针.为什么会发生变化
- 如何修复此错误:未定义对"距离(浮点数,浮点数,浮点数,浮点数,浮点数)"的引用
- 我的项目不会像"undefined reference to `grpc::g_core_codegen_interface'"那样使用未定义的引用错误进行编译
- C++Boost Asio Pool线程,带有lambda函数和传递引用变量
- 强制转换为引用类型
- 引用一个已擦除类型(void*)的指针
- 向量元素的引用地址与它所指向的向量元素的地址不同.为什么
- 具有默认值的引用获取函数
- 如何使用基类指针引用派生类成员
- 使用取消引用的指针的多态性会产生意外的结果.为什么?
- 如何引用基类的派生类?