2012-04-23 100 views
3

在我的VC++代码,这是罚款早些时候编译,我添加了一个函数X()是这样的:错误LNK2001:无法解析的外部符号C++

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文件中已经包含了头文件也正常。 但我仍没有什么原因,我收到链接错误理解:

error LNK2001: unresolved external symbol "public: virtual long __thiscall Base::X()" ([email protected]@@[email protected])

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

回答

6

您是否在Base中实施了X()?你需要做的是,或使其纯虚:

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

此外,您在DerivedX()定义看起来是错误的。你可能需要的东西是这样的:

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

是的,谢谢我摆脱了链接错误。 :)非常感谢你如此迅速的回应。 – codeLover 2012-04-23 11:34:39

2

你永远定义函数X

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

您还需要一个定义Derived::X()因为那也是virtual

+0

是的,谢谢我摆脱了链接错误。 :)非常感谢你如此迅速的回应。希望stackoverflow可以允许选择两个答案作为正确的。 :) – codeLover 2012-04-23 11:35:32

相关问题