使用纯虚方法时,VC ++会引发链接器错误

时间:2015-12-18 17:31:19

标签: c++ visual-studio linker

我遇到这种情况:

  • C ++ 项目定义了一个基类BaseClass,其中包含一些纯虚方法
  • C ++ DLL 项目定义了DerivedClass中完全实现此类方法的派生类BaseClass
  • C ++控制台应用程序实例化DerivedClass

BaseClass的定义:

部首:

class BaseClass
{
public:
    // ...
    virtual DWORD initialize(void) = 0;
    virtual DWORD terminate(void) = 0;
};

DerivedClass的定义:

部首:

class DerivedClass : public BaseClass
{
public:
    // ...
    DWORD initialize(void);
    DWORD terminate(void);
};

CPP:

DWORD DerivedClass::initialize()
{
    //...
    return 0;
}

DWORD DerivedClass::terminate()
{
    //...
    return 0;
}

控制台应用程序的作用:

DerivedClass c = DerivedClass();
c.initialize();

return 0;

虽然BaseClass和DerivedClass编译正常(因此链接配置应该很好),但控制台应用程序会编译以下错误:

error LNK2001: unresolved external symbol "public: virtual unsigned long __thiscall DerivedClass::initialize(void)" (?initialize@DerivedClass@@UAEKXZ) unresolved
error LNK2001: unresolved external symbol "public: virtual unsigned long __thiscall DerivedClass::terminate(void)" (?terminate@DerivedClass@@UAEKXZ) unresolved

无法理解为什么DerivedClass没有正确地声明这些虚拟方法?

编辑:控制台应用的Project Properties -> Linker -> Input字段为:$(OutDir)\base_class.lib;$(OutDir)\derived_class.lib;,因此从技术上讲,它引用了所有需要的库。

0 个答案:

没有答案