我正在构建一个mfc应用程序,它将使用户能够绘制图形对象(类似于ms paint)。但是由于某种原因,我收到以下链接器错误:
CElement.obj:错误LNK2001:无法解析的外部符号“public:虚拟void __thiscall CElement::Draw(class CDC *)”(?Draw @ CElement @@ UAEXPAVCDC @@@ Z)。
我知道这与CPolygon类中的虚拟绘制函数有关。但是到底是什么原因引起的呢?
//CElement.h
class CElement : public CObject
{
public:
virtual ~CElement();
virtual void Draw(CDC* pDC);
};
注意:CElement将充当所有其他类(例如CPolyline和CRectangle)的基类。 Draw函数是虚拟的-多态的一个示例,CElement的Draw(CDC * pDC)将被派生类的Draw()函数覆盖
class CPolygon : public CElement
{
public:
CPolygon(CPoint mFirstPoint,CPoint mSecondPoint);
~CPolygon(void);
virtual void Draw(CDC* pDC);
---------------------------------------------------------------------------------------
//CElement.cpp
#include "CElement.h"
//constructors for the class
void CPolygon::Draw(CDC* pDC)
{
pDC->MoveTo(mStartPoint);
pDC->LineTo(mEndPoint);
}
最佳答案
错误消息说您尚未为函数定义主体
virtual void Draw(CDC* pDC);
定义它或使类抽象化,即派生类必须实现它。
virtual void Draw(CDC* pDC) { }
要么
virtual void Draw(CDC* pDC) = 0;
关于mfc - 我将如何处理此链接器错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13819198/