派生类中构造函数和析构函数的 C++ LNK2019 错误

标签 c++ inheritance constructor destructor lnk2019

我有两个类,一个继承自另一个。编译时出现以下错误:

Entity.obj : error LNK2019: unresolved external symbol "public: __thiscall Utility::Parsables::Base::Base(void)" (??0Base@Parsables@Utility@@QAE@XZ) referenced in function "public: __thiscall Utility::Parsables::Entity::Entity(void)" (??0Entity@Parsables@Utility@@QAE@XZ)

Entity.obj : error LNK2019: unresolved external symbol "public: virtual __thiscall Utility::Parsables::Base::~Base(void)" (??1Base@Parsables@Utility@@UAE@XZ) referenced in function "public: virtual __thiscall Utility::Parsables::Entity::~Entity(void)" (??1Entity@Parsables@Utility@@UAE@XZ)

D:\Programming\Projects\Caffeine\Debug\Caffeine.exe : fatal error LNK1120: 2 unresolved externals

我真的不知道发生了什么..任何人都可以看到我做错了什么吗?我正在使用 Visual C++ Express 2008。这是文件..

"include/Utility/Parsables/Base.hpp"

#ifndef CAFFEINE_UTILITY_PARSABLES_BASE_HPP
#define CAFFEINE_UTILITY_PARSABLES_BASE_HPP

namespace Utility
{
 namespace Parsables
 {
  class Base
  {
  public:
   Base( void );
   virtual ~Base( void );
  };
 }
}

#endif //CAFFEINE_UTILITY_PARSABLES_BASE_HPP

“src/Utility/Parsables/Base.cpp”

#include "Utility/Parsables/Base.hpp"

namespace Utility
{
 namespace Parsables
 {
  Base::Base( void )
  {
  }

  Base::~Base( void )
  {
  }
 }
}

"include/Utility/Parsables/Entity.hpp"

#ifndef CAFFEINE_UTILITY_PARSABLES_ENTITY_HPP
#define CAFFEINE_UTILITY_PARSABLES_ENTITY_HPP

#include "Utility/Parsables/Base.hpp"

namespace Utility
{
 namespace Parsables
 {
  class Entity : public Base
  {
  public:
   Entity( void );
   virtual ~Entity( void );
  };
 }
}

#endif //CAFFEINE_UTILITY_PARSABLES_ENTITY_HPP

“src/Utility/Parsables/Entity.cpp”

#include "Utility/Parsables/Entity.hpp"

namespace Utility
{
 namespace Parsables
 {
  Entity::Entity( void )
  {
  }

  Entity::~Entity( void )
  {
  }
 }
}

最佳答案

相关的部分是这样的:

unresolved external symbol "public: __thiscall Utility::Parsables::Base::Base(void)"

您需要为 Base::BaseBase::~Base 提供定义。声明是不够的。即使你在这两个函数中什么都不做,你也需要留下一个空的函数体,因为C++实际上需要函数存在。 C++ 将诸如虚拟表维护之类的东西放在构造函数和析构函数中,因此即使您不需要在那里做任何事情,也必须定义它们——C++ 必须在那里做一些事情。

您确定构建中包含 Base.cpp 吗?

关于派生类中构造函数和析构函数的 C++ LNK2019 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2750173/

相关文章:

c++ - 用字典实现的 python 中的 "case sequence"

c++ - 为什么我需要在成员初始化列表中重复我的基类的模板参数?

c++ - 从模板类继承的类的正确声明

c++ - 从派生类对象访问重写的基类成员

c++ - 如何在 "header file (.h)"中的现有 "C++ Project"中创建新的 "Microsoft Visual Studio Community 2015"?

C# - 从特定应用程序捕获 Windows 消息

Java 从 DLL 实例化 C++ 类

c++ - 确定C++中类构造函数的参数个数和类型?

java - Java 中的继承和构造函数

javascript - 没有新运算符的错误