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

发布于 2024-09-06 09:37:56 字数 724 浏览 2 评论 0原文

这些类中的每一个都位于单独的 dll 模块中:

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


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

在 Other.cpp 中:

#include "Derived.h"

包含与 Base.lib 的派生链接的模块(未显示导出/导入指令)。包含 Other.cpp 的模块与 Derived.lib 链接,但不与 Base.lib 链接。到目前为止,一切正常。

但是,如果我将 Base::bar() 更改为虚拟,并且不对 Derived 进行更改,则链接器在链接 Other.dll 时会抱怨:

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

我找到的唯一解决方案是在 Derived 中覆盖 Base::bar()或者让 Other.dll 也与 Base.lib 链接。这两种解决方案都不是理想的,因为它们会在服务器更改时增加客户端要求。

在这种情况下,有什么更好的方法来访问/定义基类虚拟方法吗?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

青巷忧颜 2024-09-13 09:37:56

发生的情况是,virtual Base::bar 需要位于 Derived 的 vtable 中,因为没有 Derived::bar

我不明白你关于“服务器更改时客户端要求”的评论。我不明白基础/派生如何映射到客户端/服务器。它们对我来说是两个独立的维度。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文