错误C2259:无法实例化抽象类

时间:2011-12-20 13:00:43

标签: c++ c++builder

这是Visual Studio 2010编译器的错误信息(虽然它对于micrsoft visual studio 2003的编译器没有问题)

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\src\hfgui3\userparambase.h(128) : see declaration of 'user_param::UserParamBase::readonly'
    'bool user_param::UserParamBase::readonly(void)' : is abstract
    c:\qc\qc_daq\src\hfgui3\userparambase.h(127) : see declaration of 'user_param::UserParamBase::readonly'
    'SIZE user_param::UserParamBase::winSize(void)' : is abstract
    c:\qc\qc_daq\src\hfgui3\userparambase.h(129) : see declaration of 'user_param::UserParamBase::winSize'

我的源代码如下所示:

class UserParamBase : public UserParamName
{
    public:
        virtual bool    readonly() =0;
        virtual bool    readonly(bool bReadonly)=0;
        virtual SIZE    winSize()=0;    
        virtual bool    get()=0;
        virtual void    create(CWnd* pParentWnd,const RECT& rect)=0;
        virtual void    close()=0;  
        virtual void    update()=0;
}

...
template <>
class UserParam< string > : public UserParamB2< string >
{
public:
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()) {
        char* psz=NULL;
        string s;
        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;
        s.erase(std::remove(s.begin(),s.end(),'\r'),s.end());
        this->assign(s);
    }
    return true;
}

错误消息出现在this->assign(s);语句中。

2 个答案:

答案 0 :(得分:4)

很难具体,因为你没有发布任何代码,只有未格式化的编译器输出。但是,您似乎使用抽象方法readonly(bool)winSize(void)创建了类的子类,并且您没有在子类中实现这些函数。因此,您的子类仍然是抽象的。实施这些方法。

答案 1 :(得分:4)

您无法实例化abstract课程,也没有人可以帮助您实现这一目标。

你可以:

  • 实施class,但不再是抽象的。

  • class派生出来,让孩子实现所有抽象方法 - 这似乎就是你要做的事情。

抽象方法看起来像:

virtual void foo() = 0;

在您的情况下,方法UserParamBase::readonly(void)UserParamBase::winSize(void)在基类中是抽象的。

您必须在UserParamB2中覆盖它们并提供实施。