【问题标题】:error LNK2001: unresolved external symbol C++错误 LNK2001:未解析的外部符号 C++
【发布时间】:2012-05-03 23:49:51
【问题描述】:

在我之前编译良好的 VC++ 代码中,我添加了一个函数 X(),如下所示:

In the file BaseCollection.h
class Base
{
// code
virtual HRESULT X();
//code
};


IN the file DerivedCollection.h
class Derived:public Base
{
    HRESULT X();

}

In the file DerivedCollection.cpp
HRESULT Derived::X
{
// definition of Derived here. 
}

已在 .cpp 文件中正确包含头文件。 但我仍然不明白我收到链接错误的原因是什么:

错误 LNK2001:未解析的外部符号“公共:虚拟长 __thiscall Base::X()" (?X@Base@@UAEJI@Z)

我真的在努力修复这个错误。任何人都可以帮助我解决这个问题。 提前非常感谢。

【问题讨论】:

    标签: c++ visual-c++ inheritance linker


    【解决方案1】:

    您是否在Base 中实现了X()?你需要这样做,或者让它成为纯虚拟的:

    class Base
    {
    // code
    virtual HRESULT X() = 0; //pure virtual. Base doesn't need to implement it.
    //code
    };
    

    另外,您在Derived 中对X() 的定义看起来是错误的。你可能需要这样的东西:

    HRESULT Derived::X()
    {
    // definition of Derived here. 
    }
    

    【讨论】:

    • 是的,谢谢我摆脱了链接错误。 :) 非常感谢你如此迅速的反应。
    【解决方案2】:

    你永远不会定义函数X

    HRESULT Base::X()
    {
    // definition of X 
    }
    

    您还需要Derived::X() 的定义,因为它也是virtual

    【讨论】:

    • 是的,谢谢我摆脱了链接错误。 :) 非常感谢你如此迅速的反应。希望stackoverflow可以允许选择两个答案作为正确的答案。 :)
    猜你喜欢
    • 1970-01-01
    • 2014-09-27
    • 2018-08-14
    • 2018-08-24
    • 2020-08-21
    • 2016-09-04
    • 2013-08-19
    相关资源
    最近更新 更多