【问题标题】:How would I handle this linker error?我将如何处理此链接器错误?
【发布时间】:2012-12-11 11:16:37
【问题描述】:

我正在构建一个 mfc 应用程序,使用户能够绘制图形对象(类似于 ms paint)。但由于某种原因,我收到以下链接器错误:

CElement.obj:错误 LNK2001:无法解析的外部符号“public:virtual 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);

}

【问题讨论】:

    标签: mfc error-handling polymorphism linker-errors


    【解决方案1】:

    正如错误消息所说,您还没有为函数定义主体

    virtual void Draw(CDC* pDC);
    

    要么定义它,要么使类抽象,即派生类必须实现它。

    virtual void Draw(CDC* pDC) { }
    

    virtual void Draw(CDC* pDC) = 0;
    

    【讨论】:

      猜你喜欢
      • 2012-07-18
      • 2014-01-10
      • 2014-04-05
      • 1970-01-01
      • 1970-01-01
      • 2010-09-19
      • 2015-06-09
      • 1970-01-01
      相关资源
      最近更新 更多