0

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

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

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

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

2 回答 2

2

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

于 2013-06-27T14:17:09.460 回答
1

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

于 2013-06-27T14:25:17.513 回答