简体   繁体   中英

Unresolved external symbol on virtual function

Each of these classes are in separate dll modules:

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


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

In Other.cpp:

#include "Derived.h"

The module that contains Derived links with Base.lib (not shown are the export/import directives). The module that contains Other.cpp links with Derived.lib, but not 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:

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. 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 .

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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