虚函数上未解析的外部符号
这些类中的每一个都位于单独的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
发生的情况是,
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 noDerived::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.