【问题标题】:error C2259:cannot instantiate abstract class错误 C2259:无法实例化抽象类
【发布时间】:2011-12-20 13:00:42
【问题描述】:

这是 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-&gt;assign(s); 语句中。

【问题讨论】:

  • 无法实例化抽象类。编译器错误信息没有错。你无法“修复”它。
  • 注意非常有帮助,因为我可以通过向类添加接口来触发此错误...

标签: c++ c++builder


【解决方案1】:

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

【讨论】:

  • 非常感谢您的回复。
  • @Cody Gray:请告诉我如何修复它。因为这段代码在 Microsoft Visual Studio 2003 上编译得很好,但在 2010 上编译得不好。
【解决方案2】:

您无法实例化 abstract 类,没有人可以帮助您做到这一点。

您可以:

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

  • class 派生,并让孩子实现所有抽象方法 - 看来这就是您想要做的。

抽象方法看起来像:

virtual void foo() = 0;

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

您必须在 UserParamB2 中覆盖它们并提供一个实现。

【讨论】:

  • 您好!非常感谢您回复我的帖子!
  • 但我无法找出正确的代码,请您再帮我一些忙。我在这里发布代码。
  • error C2259: 'user_param::UserParamB2' : 由于以下成员,无法实例化抽象类:
  • @user1107855 同样的问题 - 你没有覆盖所有抽象方法。
  • 我是 VC++ 新手,请提供有用的建议如何摆脱这个问题。由于此代码已经在 Microsoft Visual Studio 2003 中编译,但在 Visual Studio 2010 中尚未编译。
猜你喜欢
  • 1970-01-01
  • 2015-03-25
  • 2012-08-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-05-13
  • 2019-03-13
  • 1970-01-01
相关资源
最近更新 更多