错误LNK2019:派生类中未解析的外部符号

时间:2013-06-27 14:11:54

标签: c++

当我在另一个项目中调用派生类的构造函数时发生错误。我省略了代码中的一些细节。我正在使用Visual Studio 2012。

- 基类/派生类和测试文件位于两个不同的项目中。可以毫无问题地编译基类/派生类。

- 注释构造函数行时,可以成功编译Test项目。

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

2 个答案:

答案 0 :(得分:2)

最简单的答案是您没有使用main构建并包含实现文件,因此链接器无法找到Child构造函数的代码。在MSDN帮助中查找该特定错误代码并检查那里的所有可能性。

答案 1 :(得分:1)

如果你想在另一个项目中使用这些类,那么你要么包含整个源(头文件和cpp文件)并构建它们,要么从DLL项目中导出它们并将它们导入到其他项目中。

相关问题