在另一个项目中调用派生类的构造函数时出现错误。我在代码中省略了一些细节。我正在使用 Visual Studio 2012。
-基/派生类和测试文件在两个不同的项目中。可以毫无问题地编译基类/派生类。
-注释掉constructor行就可以编译成功。
-Test.cpp 与 DerivationFunction 文件中的其他构造函数配合良好。
// Test.cpp
#include "DerivationFunction.h"
Child con(123, 123); // error LNK2019: unresolved external symbol "public: __thiscall Child::Child(unsigned short,unsigned int)" (??Child@@QAE@GI@Z) referenced in function _main
基类和派生类的头文件:
// DerivationFunction.h
class Base
{
public:
virtual void AppendEnums() = 0;
static int CopyBuffer();
uint16 GetFeatureID();
protected:
uint16 baseValue;
static int Copy();
};
// Child class
class Child : public Base
{
public:
uint32 childValue;
Child(uint16 featureID, uint32 value);
void AppendEnums();
};
源文件:
// DerivationFunction.cpp
int Base::CopyBuffer()
{
return 0;
}
uint16 Base::GetFeatureID()
{
return baseValue;
}
int Base::Copy()
{
return 0;
}
// Child class
Child::Child(uint16 featureID, uint32 value)
{
baseValue = featureID;
childValue = value;
}
void Child::AppendEnums()
{
}
最佳答案
最简单的答案是您还没有在主程序中构建和包含实现文件,因此链接器无法找到 Child
构造函数的代码。在 MSDN 帮助中查找该特定错误代码并检查所有可能性。
关于c++ - 错误 LNK2019 : unresolved external symbol in derived class,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17345521/