错误LNK2001:未解析的外部符号C++

cod*_*ver 5 c++ inheritance linker visual-c++

在我之前编译得很好的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. 
}
Run Code Online (Sandbox Code Playgroud)

已将头文件也正确包含在.cpp文件中.但我仍然不明白为什么我收到链接错误:

错误LNK2001:未解析的外部符号"public:virtual long __thiscall Base :: X()"(?X @ Base @@ UAEJI @ Z)

我真的很努力地修复这个bug.任何人都可以帮助我解决这个问题.非常感谢提前.

jua*_*nza 7

你实施X()了Base吗?您需要这样做,或使其成为纯虚拟:

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

此外,你的定义X()中Derived看起来是错误的.你可能需要这样的东西:

HRESULT Derived::X()
{
// definition of Derived here. 
}
Run Code Online (Sandbox Code Playgroud)