7

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

Entity.obj:错误 LNK2019:未解析的外部符号“public:__thiscall Utility::Parsables::Base::Base(void)”(??0Base@Parsables@Utility@@QAE@XZ) 在函数“public: __thiscall Utility”中引用::Parsables::Entity::Entity(void)" (??0Entity@Parsables@Utility@@QAE@XZ)

Entity.obj:错误 LNK2019:未解析的外部符号“public:virtual __thiscall Utility::Parsables::Base::~Base(void)”(??1Base@Parsables@Utility@@UAE@XZ) 在函数“public:虚拟 __thiscall Utility::Parsables::Entity::~Entity(void)" (??1Entity@Parsables@Utility@@UAE@XZ)

D:\Programming\Projects\Caffeine\Debug\Caffeine.exe : 致命错误 LNK1120: 2 unresolved externals

我真的不知道发生了什么.. 谁能看到我做错了什么?我正在使用 Visual C++ Express 2008。这是文件..

“包括/实用程序/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/实用程序/Parsables/Base.cpp”

#include "Utility/Parsables/Base.hpp"

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

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

“包括/实用程序/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/实用程序/Parsables/Entity.cpp”

#include "Utility/Parsables/Entity.hpp"

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

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

3 回答 3

9

相关位是这样的:

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

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

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

于 2010-05-01T14:02:02.210 回答
1

今天刚刚在 Visual Studio 2015 中遇到了同样的错误。不幸的是,接受的答案不起作用(以及许多相同问题的答案)。对我有用的是右键单击基类 cpp 文件,排除然后再次包含它。我认为 VS 在移动文件和重命名时不知何故感到困惑,它只是默默地拒绝编译它,即使它在属性编辑器中标记为“包含在项目中”= true 以及在组中的 vcproj 文件中列出。这是一个可怕的错误,最终花了很多时间在上面。

于 2016-10-11T09:55:47.620 回答
0

您的 base.cpp 没有被编译/链接,或者您的拼写错误

于 2010-05-01T14:07:15.080 回答