c++ - 错误 LNK2019 : unresolved external symbol in derived class

标签 c++

在另一个项目中调用派生类的构造函数时出现错误。我在代码中省略了一些细节。我正在使用 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/

相关文章:

c++ - 对于大型桌面应用程序,Qt Quick 是否足够成熟?

c++ - Automake 库工具 'No rule to make target'

c++ - 如何避免将类成员传递给回调的类似 const 和非 const 成员函数之间的代码重复?

c++ - 将异构初始化器列表传递给流运算符

C++/C语言独立内存分配

C++11 具有非原子变量的原子内存顺序

C++ 数组和大小

c++ - 如何将 std::vector<std::string> 作为 char** 传递?

c++ - 检测类型是否为零元素数组?

c++ - 像 typedef 一样使用 decltype