【问题标题】:Peculiar Virual Function Fixes Unresolved External Symbol特殊的病毒函数修复了未解析的外部符号
【发布时间】: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


【解决方案1】:

已解决:

问题在于,在 OtherClass.h 中,当它是一个类时,Config 被前向声明为结构。它不会在 vs2010 中抱怨这一点,但在 vs2017 中会抱怨

【讨论】:

    猜你喜欢
    • 2020-02-15
    • 2018-05-23
    • 2014-04-20
    • 2013-11-03
    • 2013-03-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多