我有两个类,一个继承自另一个。编译时出现以下错误:
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::Base
和 Base::~Base
提供定义。声明是不够的。即使你在这两个函数中什么都不做,你也需要留下一个空的函数体,因为C++实际上需要函数存在。 C++ 将诸如虚拟表维护之类的东西放在构造函数和析构函数中,因此即使您不需要在那里做任何事情,也必须定义它们——C++ 必须在那里做一些事情。
您确定构建中包含 Base.cpp 吗?
关于派生类中构造函数和析构函数的 C++ LNK2019 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2750173/