简体   繁体   English

虚函数上未解析的外部符号

[英]Unresolved external symbol on virtual function

Each of these classes are in separate dll modules: 这些类中的每一个都在单独的dll模块中:

class Base
{
public:
  virtual void foo();
  void bar();
};


class Derived : public Base
{
public:
  // override Base::foo()
  void foo();
};

In Other.cpp: 在Other.cpp中:

#include "Derived.h"

The module that contains Derived links with Base.lib (not shown are the export/import directives). 包含带有Base.lib的派生链接的模块(未显示的是export / import指令)。 The module that contains Other.cpp links with Derived.lib, but not Base.lib. 包含Other.cpp的模块与Derived.lib链接,但不与Base.lib链接。 Up to this point, everything works fine. 到目前为止,一切正常。

However, if I change Base::bar() to be virtual and do not make a change to Derived, then the linker complains when linking Other.dll: 但是,如果我将Base :: bar()更改为虚拟并且不对Derived进行更改,则链接器在链接Other.dll时会抱怨:

Other.obj : error LNK2001: unresolved external symbol "public: virtual void __thiscall Base::bar()" ...

The only solutions I have found is to override Base::bar() in Derived or to have Other.dll also link with Base.lib. 我发现的唯一解决方案是覆盖Derived中的Base :: bar()或让Other.dll也与Base.lib链接。 Both solutions are not desired because they add client requirements when the server changes. 不需要这两种解决方案,因为它们会在服务器更改时增加客户端要求。

Any ideas on a better way to access/define the base class virtual methods in this scenario? 在这种情况下,有什么更好的方法来访问/定义基类虚拟方法的想法吗?

What's happening is that virtual Base::bar needs to be in the vtable of a Derived, since there's no Derived::bar . 发生的事情是virtual Base::bar必须位于Derived的vtable中,因为没有Derived::bar

I don't understand your comment about "client requirements when the server changes". 我不理解您对“服务器更改时的客户端要求”的评论。 I don't see how base/derived maps to client/server. 我看不到基本/派生到客户端/服务器的映射。 They're two independent dimensions to me. 对我来说,它们是两个独立的维度。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM