C++派生类中的构造函数和析构函数出现 LNK2019 错误
我有两个类,一个是从另一个继承的。当我编译时,我收到以下错误:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
相关部分是这样的:
您需要为
Base::Base
和Base::~Base
提供定义。声明还不够好。即使你在这两个函数中没有任何事情可做,你也需要留一个空的函数体,因为C++实际上要求函数存在。 C++ 将诸如虚拟表维护之类的东西放在构造函数和析构函数中,因此即使您不需要在那里做任何事情,也必须定义它们——C++ 必须在那里做事情。您确定 Base.cpp 包含在构建中吗?
The relevant bit is this:
You need to provide a definition for
Base::Base
andBase::~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?
今天刚刚在 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.
您的 base.cpp 没有被编译/链接,或者您的拼写错误
Either your base.cpp is not being compiled/linked or you have a misspelling in it