C++ 函数中未定义的引用c++;

C++ 函数中未定义的引用c++;,c++,class,C++,Class,我总是犯一个奇怪的错误 /usr/lib/gcc/x86_64-linux-gnu/5/./../../../x86_64-linux-gnu/crt1.o: 在函数_start:>(.text+0x20)中:未定义对main的引用 /tmp/cc4ZqKzy.o: 在函数'Sep::Building::Building(Sep::Field::FieldType,>std::uu cxx11::basic_string,>std::allocator>,char,bool,bool,unsig

我总是犯一个奇怪的错误

/usr/lib/gcc/x86_64-linux-gnu/5/./../../../x86_64-linux-gnu/crt1.o:

在函数_start:>(.text+0x20)中:未定义对main的引用 /tmp/cc4ZqKzy.o:

在函数'Sep::Building::Building(Sep::Field::FieldType,>std::uu cxx11::basic_string,>std::allocator>,char,bool,bool,unsigned int,unsigned int)中:

Building.cpp:(.text+0x3c):对Sep::Field::Field()的未定义引用 集合2:

错误:ld返回了1个退出状态

我读了很多关于这个问题的书,但没有一本是同样的。我包括了所有的标题,还添加了ifndef防护

main.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';
}
Building.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_;
};
#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(){};
#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_;
};
有人有主意吗?因为我经常在这个项目中遇到这个错误,但是在其他类中。 奇怪的是,程序似乎编译正确,但一开始我就得到了这个collect2:error:ld返回了1退出状态


Thx

当您试图构造一个
建筑时,
建筑::建筑(…)
构造函数隐式调用其基类构造函数
字段::字段()
(因为您没有指定所需的
字段
构造函数)。您在
Field.h
中承诺这样的构造函数存在于某个地方(这就是为什么编译器从不抱怨),但您从未定义它。然后,当链接器尝试将您声明的函数与编译器发出的函数链接时,它会注意到缺少此构造函数并发出投诉

这就是错误消息试图告诉您的内容:

  • 对“Sep::Field::Field()”的未定义引用
    ->

  • 在函数Sep::Building::Building(…)
    ->中,它试图调用所示的
    Building
    构造函数中的
    字段
    构造函数

最简单的修复方法是写入
Field()=default
编辑:如果要使用
Field::Field(FieldType)
构造函数,可以这样做:

Building::Building(FieldType fieldType, /* etc */)
  : Field(fieldType)
{
  // etc.
}
您还可以将构造函数添加到
字段
类中,该类接受您尝试传递的所有这些参数:

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. */)
{
}
更好的是,您可以“重用”用于
构建的长
字段
构造函数

class Building : public Field
{
public:
  using Field::Field;

  // ...
}

当您试图构造
建筑
时,
建筑::建筑(…)
构造函数会隐式调用其基类构造函数
字段::字段()
(因为您没有指定所需的
字段
构造函数)。您在
Field.h
中承诺这样的构造函数存在于某个地方(这就是为什么编译器从不抱怨),但您从未定义它。然后,当链接器尝试将您声明的函数与编译器发出的函数链接时,它会注意到缺少此构造函数并发出投诉

这就是错误消息试图告诉您的内容:

  • 对“Sep::Field::Field()”的未定义引用
    ->

  • 在函数Sep::Building::Building(…)
    ->中,它试图调用所示的
    Building
    构造函数中的
    字段
    构造函数

最简单的修复方法是写入
Field()=default
编辑:如果要使用
Field::Field(FieldType)
构造函数,可以这样做:

Building::Building(FieldType fieldType, /* etc */)
  : Field(fieldType)
{
  // etc.
}
您还可以将构造函数添加到
字段
类中,该类接受您尝试传递的所有这些参数:

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. */)
{
}
更好的是,您可以“重用”用于
构建的长
字段
构造函数

class Building : public Field
{
public:
  using Field::Field;

  // ...
}

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(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_;
};
#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(){};
#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_;
};
#包括“Field.h”
#包括
#包括
#包括
#包括
使用Sep::Field;
//------------------------------------------------------------------------------
//将私有FieldType类型设置为给定参数的Setter
//
//@param要设置的字段类型
//
字段::字段(){
//空构造函数或可以将类型\初始化为默认值。
}
字段::字段(字段类型)
{
类型=类型;
};
字段::~Field(){};
//------------------------------------------------------------------------------
//检查给定字段的类型,将类型名称作为字符串返回
//
//@param type,要检查的字段的类型
//
//@return string选中字段类型的名称
//
std::string字段::getName(字段类型)
{
开关(类型)
{
案例草:
返回std::string(“Grass”);
案例水:
返回标准::字符串(“水”);
案例障碍:
返回标准::字符串(“障碍”);
凯斯街:
返回标准::字符串(“街道”);
案例之家:
返回std::字符串(“Home”);
案例市场:
返回标准::字符串(“市场”);
个案诊所:
返回std::字符串(“Clinic”);
凯斯市政厅:
返回标准::字符串(“市政厅”);
违约:
返回标准::字符串(“未知字段”);
}
};
//------------------------------------------------------------------------------
//吸气剂
//
//来自私有字段类型的Getter_
//
//@param-none
//
//@将type的类型返回为FieldType
//
Field::FieldType Field::getType()常量
{
返回类型;
};

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(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_;
};
#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(){};
#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_;
};
#包括“Field.h”
#包括
#包括
#包括
#包括
使用Sep::Field;
//-------------------