对vtable的未定义引用(出于想法)

时间:2013-07-26 06:00:58

标签: c++ oop

我已经完成了我在网上找到的所有东西,但似乎我错过了一些非常基本的东西,花了这么多时间来解决这个问题并没有让事情变得更容易,所以我会继续在这里寻求帮助,看看你能否找到我做错的事。

以下是代码:

jControl.h

#ifndef _JCONTROL_H
#define _JCONTROL_H

namespace view
{


class jControl
{

public:


HWND hwnd;
HWND hParent;
HBITMAP hbitmap;
std::string text;
HFONT hFont;
COLORREF textColor;
COLORREF backgroundColor;
RECT updateRegion;
bool isUpdateRegion;

public:


jControl (  );

jControl ( HWND parent, HINSTANCE hInstance, WORD bitmap );

~jControl (  );

virtual void show (  );

virtual void hide (  );

virtual void disable (  );

virtual void enable (  );

virtual std::string getText (  );

virtual void setText ( std::string txt );

virtual void setUpdateRegion ( RECT rect );

virtual void setTextColor ( COLORREF crf );

virtual void setBackgroundColor ( COLORREF crf );

virtual void setFont ( HFONT font );

virtual bool setRange ( int range );

virtual bool setStep ( int step );

};

};

#endif

jControl.cpp

#include "jControl.h"

namespace view
{



jControl::jControl(  ){};


jControl::jControl ( HWND parent, HINSTANCE hInstance, WORD bitmap ) 
                    : hParent ( parent ), hbitmap ( NULL ), isUpdateRegion ( false ), textColor (  RGB ( 255, 255, 255 )  ), backgroundColor (  RGB ( 54, 54, 54 )  )
{

    hFont = ::CreateFont ( 18, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, "Myriad Pro" );

    if ( bitmap != 0 )  hbitmap = LoadBitmap (  hInstance, MAKEINTRESOURCE ( bitmap )  );

};


jControl::~jControl (  ) 
{ 

  if ( hbitmap )    DeleteObject ( hbitmap );

  if ( hFont )      DeleteObject ( hFont );

  if ( hwnd )       DestroyWindow ( hwnd ); 

};

void jControl::show(  ) {   ::ShowWindow( hwnd, SW_SHOW );  };

void jControl::hide(  ) {   ::ShowWindow( hwnd, SW_HIDE );  };

void jControl::disable (  ) {   EnableWindow ( hwnd, false );   };

void jControl::enable (  )  {   EnableWindow ( hwnd, true );    };

std::string jControl::getText (  )  {   return text;    };

void jControl::setText ( std::string txt )  {   text = txt;     InvalidateRect ( hwnd, NULL, true );    };

void jControl::setUpdateRegion ( RECT rect )    {   updateRegion = rect;    isUpdateRegion = true;  };

void jControl::setTextColor ( COLORREF crf )    {  textColor = crf;  };

void jControl::setBackgroundColor ( COLORREF crf )  {  backgroundColor = crf; ::InvalidateRect ( hwnd, NULL, true );  };

void jControl::setFont ( HFONT font )   {   hFont = font;   };

bool jControl::setRange ( int range )   {   return true;    };

bool jControl::setStep ( int step )     {   return true;    };

};

jProgressBar.h

#ifndef _JPROGRESSBAR_H
#define _JPROGRESSBAR_H




/**     * Parent class include      */
#ifndef _JCONTROL_H
   #include  "../jControl/jControl.h"
#endif


namespace view
{

class jProgressBar : public jControl
{


public:

jProgressBar ( std::string txt, int x, int y, int width, int height, HWND parent, HINSTANCE hInstance, WORD bitmap );

~jProgressBar (  );

std::string getText (  );

void setText ( std::string txt );

bool setRange ( int range );

bool setStep ( int step );


};

};

#endif

jProgressBar.cpp

#include "jProgressBar.h"

namespace view
{


jProgressBar::jProgressBar( std::string txt, int x, int y, int width, int height, HWND parent, HINSTANCE hInstance, WORD bitmap ) 
                  : jControl( parent, hInstance, bitmap )
{

    hwnd =  CreateWindowEx ( WS_EX_TOPMOST, PROGRESS_CLASS, txt.c_str(), WS_CHILD | WS_CLIPSIBLINGS | WS_CHILD, x, y, width, height, parent, 0, hInstance, 0 );

};

jProgressBar::~jProgressBar (  ){};

bool jProgressBar::setRange ( int range )
{ 

    if ( SendMessage (  hwnd, PBM_SETRANGE, 0, MAKELPARAM ( 0,  range )  ) != 0 )   return true;

    return false;

};

bool jProgressBar::setStep ( int step )
{ 

     if ( SendMessage (  hwnd, PBM_SETSTEP, ( WPARAM ) step, 0  ) != 0 )    return true;

    return false;

};


};

这是错误:

[Linker error] jProgressBar/jProgressBar.cpp:14: undefined reference to `vtable for view::jProgressBar' 

我在JProgressBar的构造函数和析构函数中得到错误。

1 个答案:

答案 0 :(得分:5)

问题可能在于以下几点:

std::string getText(  ); 

void setText( std::string txt );

在jProgressBar.h中

请在Cpp中提供此函数的定义或从头中删除它,我认为它应该编译。主要原因是:您已使用声明覆盖子类中的虚函数,但尚未给出该方法的定义。 编译器知道该函数,但链接器无法找到该定义 例如:

class Base
{
    virtual void f() = 0;
}
class Derived : public Base
{
    void f();
}