【发布时间】:2020-08-14 19:07:52
【问题描述】:
我只是继承了一些遗留代码。调用问题方法的位置。我在底部有两个解决方案,但是为什么要让 issueMethod 虚拟工作...
OtherClass::someOtherMethod() {
// problemMethod(Manager* manager, const ConfigClass* config)
obj->problemMethod(man, conf); // conf is not const in this file which therefore does not match the params list.
}
现在在 obj .h 和 .cpp 中,该方法的格式为(我检查了 cpp 和 h 都匹配)。
problemMethod(Manager* manager, const ConfigClass* config);
在尝试构建时,它在 OtherClass::SomeOtherMethod 中有一个未解析的外部,不知道问题方法在哪里。
两种解决方法:改变.h和.cpp中的problemMethod,去掉const。哪个工作,构建和运行。或者将问题方法设为虚拟。为什么让它成为虚拟的工作?我的猜测是,因为它是虚拟的,所以它会延迟到运行时,这将防止链接器错误,然后它只是稍后才弄清楚......?链接器错误令人困惑的部分是,Visual Studio 在该方法上按 F12 时确实会找到它并知道它在哪里。
【问题讨论】:
-
提供重现问题的最小程序。
-
关于链接器错误的令人困惑的部分是,Visual Studio 在该方法上按 F12 时确实找到它并知道它在哪里。 这真的与链接器可以访问的符号无关。
标签: c++ linker virtual-functions