VS2010错误2259和lnk 2001中的编译错误

时间:2012-01-19 14:10:42

标签: c++ visual-studio-2010

class UserParamBase : public UserParamName
{
public:

inline UserParamBase() : m_pParent(NULL), m_bCreate(false), m_bLoad(false),  m_pParentWnd(NULL) {}
inline UserParamBase(const char* name) : UserParamName(name), m_pParent(NULL), m_bCreate(false),  m_pParentWnd(NULL) {}
inline UserParamBase(const UserParamBase&) : m_pParent(NULL), m_bCreate(false),  m_pParentWnd(NULL) {}      //define the copy constructor so as not to use the default
UserParamBase& operator=(const UserParamBase&) { return *this; }    //define the operator= so as not to use the default which would copy a pointer
virtual ~UserParamBase();   
void            attach(UserParamReg* page); 
void            PlaceCtrl(UserParamReg* page, CWnd* pParentWnd, const RECT & r, bool bLoad=true);
void            CreateCtrl()                {create(m_pParentWnd, m_rRegion);};
void            CloseCtrl()                 {close();};
virtual string& value(string& s)        = 0;
virtual bool    load(const string& s)   = 0;
virtual bool    readonly()               =0;        
virtual bool    readonly(bool bReadonly) =0;        
virtual SIZE    winSize()                =0;        
virtual bool    get()                    =0;        
virtual void    update()                 =0;        
virtual void    create(CWnd* pParentWnd,const RECT& rect) =0;   
virtual void    close()                                   =0;

inline bool     loadable() const { return m_bLoad; }

protected:
virtual void    set(bool bAsync=true);

private:

UserParamReg*       m_pParent;
bool                m_bCreate;
bool                m_bLoad;


protected:
RECT                m_rRegion;
CWnd *              m_pParentWnd;
CritSec             m_csWnd;
};

template <class T>
class UserParam : public UserParamB2<T>
{
public:
template <>
class UserParam< string > : public UserParamB2< string >
{
public:
static const LONG   labelHeight             = 16;
static const LONG   controlWidth            = 160;
static const LONG   controlHeightMultiline  = 60;
static const LONG   controlHeightSingleline = 18;
static const LONG   controlWidthFileButton  = 18;
static const bool   defaultMultiline        = true;
static const bool   defaultFileEdit         = false;
static const bool   defaultReadonly         = false;
static const bool   defaultAlignRight       = false;
enum {nIDFileButton=1000, nIDEdit};


inline UserParam() {}
inline UserParam(const type& t,const char* name=NULL)                   : UserParamB2<type>(t,name) {}
inline UserParam(const UserParam& t,const char* name=NULL)              : UserParamB2<type>(t,name) {}
template <class Y> inline UserParam(const Y& y,const char* name=NULL)   : UserParamB2<type>(y,name) {}

inline UserParam& operator=(const UserParam& c) { UserParamB2<type>::operator=(c); return *this; }

bool fileedit(bool bFileEdit) { AutoCritSec acsWnd(m_csWnd, true); return m_wnd.bFileEdit=bFileEdit; }

bool combobox(bool bComboBox=true) { AutoCritSec acsWnd(m_csWnd, true); return m_wnd.bComboBox=bComboBox; }

bool multiline()        { AutoCritSec acsWnd(m_csWnd, true); return m_wnd.bMultiline; }

bool multiline(bool bMultiline) { AutoCritSec acsWnd(m_csWnd, true); return m_wnd.bMultiline=bMultiline; }

void addtolist(string str) { AutoCritSec acsWnd(m_csWnd, true); m_wnd.vSelections.push_back(str); fillcombo();}

inline void alignRight(bool bAlignRight)    { AutoCritSec acsWnd(m_csWnd, true); m_wnd.bAlignRight = bAlignRight; }

inline bool alignRight(){ AutoCritSec acsWnd(m_csWnd, true); return m_wnd.bAlignRight; }

bool readonly()  { AutoCritSec acsWnd(m_csWnd, true); return m_wnd.readonly; }

bool readonly(bool bReadonly)
{
    AutoCritSec acsWnd(m_csWnd, true);
    if(bReadonly==m_wnd.readonly) return m_wnd.readonly;
    if(m_wnd.wnd && m_wnd.wnd->GetSafeHwnd()) {
        if(bReadonly) m_wnd.wnd->PostMessage(WM_CANCELMODE);
        m_wnd.wnd->PostMessage(WM_ENABLE,(WPARAM)!bReadonly);
    }
    return m_wnd.readonly=bReadonly;
}

SIZE winSize() 
{
    SIZE p;
    p.cx=controlWidth;
    p.cy=labelHeight+((m_wnd.bMultiline && !m_wnd.bComboBox)? controlHeightMultiline : controlHeightSingleline);
    return p;
}

//get from control, validate data, set data member if valid;
//return true if data is valid, false otherwise
bool get()
{
    AutoCritSec acsWnd(m_csWnd, true);
    if(m_wnd.combobox && m_wnd.combobox->GetSafeHwnd()) {
        CString text;
        m_wnd.combobox->GetWindowText(text);
        this->assign((LPCSTR) text);

    } else if(m_wnd.wnd && m_wnd.wnd->GetSafeHwnd()) {

        //get value from control, set m_d
        char* psz=NULL;

        unsigned uSize = m_wnd.wnd->GetWindowTextLength()+1;
        try {
            psz=new char[uSize];
            m_wnd.wnd->GetWindowText(psz,uSize);
            s.assign(psz);
        }
        catch(...) {
            if(psz) delete [] psz;
            throw;
        }
        if(psz) delete [] psz;

        //remove '\r' chars; std::remove returns an iterator to the new .end() position
        //use s.erase(..) to erase from this new .end() position through then end of s
        s.erase(std::remove(s.begin(),s.end(),'\r'),s.end());
        this->assign(s);
    } 

    return true;
}

错误消息如下:

c:\qc\qc_daq_development\qc\qc_daq\src\hfgui3\userparam.h(437): error C2259: 
'user_param::UserParamB2<std::string>' : cannot instantiate abstract class
 due to following members:   

 'bool user_param::UserParamBase::readonly(bool)' : is abstract
 c:\qc\qc_daq_development\qc\qc_daq\src\hfgui3\userparambase.h(126) : see   
 declaration of 'user_param::UserParamBase::readonly'
 'bool user_param::UserParamBase::readonly(void)' : is abstract

 c:\qc\qc_daq_development\qc\qc_daq\src\hfgui3\userparambase.h(125) : see 
 declaration of 'user_param::UserParamBase::readonly'
 'SIZE user_param::UserParamBase::winSize(void)' : is abstract

 c:\qc\qc_daq_development\qc\qc_daq\src\hfgui3\userparambase.h(127) : see  
 declaration of 'user_param::UserParamBase::winSize'
 'bool user_param::UserParamBase::get(void)' : is abstract 

  c:\qc\qc_daq_development\qc\qc_daq\src\hfgui3\userparambase.h(130) : see  
  declaration of 'user_param::UserParamBase::get'
  'void user_param::UserParamBase::update(void)' : is abstract

  c:\qc\qc_daq_development\qc\qc_daq\src\hfgui3\userparambase.h(131) : see
  declaration of 'user_param::UserParamBase::update'
  'void user_param::UserParamBase::create(CWnd *,const RECT &)' : is abstract

  c:\qc\qc_daq_development\qc\qc_daq\src\hfgui3\userparambase.h(132) : see
  declaration  of 'user_param::UserParamBase::create'
  'void user_param::UserParamBase::close(void)' : is abstract

  c:\qc\qc_daq_development\qc\qc_daq\src\hfgui3\userparambase.h(133) : see 
  declaration  of 'user_param::UserParamBase::close'

当我尝试使用覆盖函数覆盖纯虚函数时,没有错误,但最后我得到了很多链接错误lnk2001。请帮我解决这个问题。提前谢谢了。如果我错过了某些内容或者某些内容似乎不清楚,请告诉我。虽然VS 2003再次没有这样的问题,但这种情况在2010年发生。

1 个答案:

答案 0 :(得分:0)

抽象类成员函数意味着在派生类中被覆盖。它们的目的是在不提供实现的情况下声明接口。因此,当您在任何类声明中看到类似virtual int foo() = 0;的内容时,这意味着您必须创建派生类并提供foo()实现才能使用此类:

class CBase {
public:
virtual int foo() = 0;
virtual double bar() = 0;
}

class CDerived : public CBase {
public:
virtual int foo();  //no more `= 0` here cause implementation is provided
virtual double bar();
};

int CDerived::foo()
{
     return 0;
}

double CDerived::bar()
{
     return 0.5;
}

int main()
{
    CBase b; //error - abstract class
    CDerived d; //ok
}

如果CDerived中有virtual int foo() = 0并且提供函数定义,编译器应该给出错误。 VS2003编译此类代码的事实看起来像是一致性问题。

修改

您的代码中有更多错误。

对于声明为:

的类
class UserParamBase : public UserParamName
{
 /*...*/
virtual bool    readonly();
}

函数定义应为:

bool UserParamBase::readonly()
{
 /*...*/
}

很奇怪,readonly()等代码在您的情况下已成功编译。您将这些函数定义为全局(或命名空间内),因此无法访问受保护的类成员,如m_csWnd