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

发布于 2024-08-31 03:56:21 字数 1937 浏览 3 评论 0原文

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

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) 在函数中引用“公共:虚拟__thiscall实用程序::Parsables::Entity::~Entity(void)”(??1Entity@Parsables@Utility@@UAE@XZ)

D:\Programming\Projects\Caffeine\Debug\Caffeine.exe:致命错误 LNK1120:2 个无法解析的外部

我真的不知道发生了什么..任何人都可以看到我在做什么错误的?我正在使用 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 )
  {
  }
 }
}

I have two classes, one inherited from the other. When I compile, I get the following errors:

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

I really can't figure out what's going on.. can anyone see what I'm doing wrong? I'm using Visual C++ Express 2008. Here are the files..

"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 )
  {
  }
 }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

长亭外,古道边 2024-09-07 03:56:21

相关部分是这样的:

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

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

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

The relevant bit is this:

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

You need to provide a definition for Base::Base and Base::~Base. A declaration is not good enough. Even if you have nothing to do in either function, you need to leave an empty function body, because C++ actually requires the function to exist. C++ puts things like virtual table maintenance inside your constructors and destructors, so they must be defined even if you don't need to do anything there -- C++ has to do things in there.

Are you sure Base.cpp is being included in the build?

柒七 2024-09-07 03:56:21

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

Just encountered this exact same error today in Visual Studio 2015. Unfortunately the accepted answer didn't worked (as well as answers from many same questions). The thing that worked for me was right click on the base class cpp file, exclude and then include it again. I think somehow VS got confused while moving file around and renames and it just silently refused to compile it even though it was marked as "Included In project" = true in property editor as well as listed in vcproj file in group. This is horrible error and ended up spending good hour on it.

薆情海 2024-09-07 03:56:21

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

Either your base.cpp is not being compiled/linked or you have a misspelling in it

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文