塞尔维亚你好! :)当我尝试编译这个类时我有错误..问题是什么? [英] Hello from serbia! :) I have error when I try to compile this class..what is the problem?

查看:129
本文介绍了塞尔维亚你好! :)当我尝试编译这个类时我有错误..问题是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

错误:无效转换为抽象类类型'Control'|

,因为以下虚拟函数在'Control'中是纯粹的:|



这是我的代码

error: invalid cast to abstract class type 'Control'|
because the following virtual functions are pure within 'Control':|

This is my code

#ifndef BUTTON_HPP_INCLUDED
#define BUTTON_HPP_INCLUDED

#include "Control.hpp"
class Button:public Control{
private:
    DinString caption;
    int clicks=0;
public:
    Button() {}
 Button(int i,int w,int h,DinString cap,int cl):Control(i,w,h),caption(cap),clicks(cl){}
 Button(const Button& b):Control((Control)b),caption(b.caption),clicks(0){}
void click(){
   clicks++;
}
void log()const{

    cout<<"Button2-Save"<<id<<"w: "<<width<<"H: "<<height<<"clicks: "<<clicks<<endl;

}

DinString getCaption() const {
    return caption;
}

void setCaption(DinString ds) {
    caption = ds;
}

int getClicks() const {
    return clicks;
}

};
#endif // BUTTON_HPP_INCLUDED







我尝试了什么:



另一个simmilar代码正在工作,但这个没有。我真的不明白是什么问题,因为我认为一切都是好写的




What I have tried:

the another simmilar code is working,but this no.I really dont understand what is the problem,because i think everything is good written

推荐答案

Button 类必须覆盖控制类(您没有向我们展示 Control.hpp 代码)。例如,请参阅 C ++中的纯虚函数和抽象类
The Button class must override all the pure virtual functions declared in the Control class (you didn't show us Control.hpp code). See, for instance Pure Virtual Functions and Abstract Classes in C++.


不,它们编写不正确。此行使Control成为一个抽象类:
No, they are not written correctly. This line makes Control an abstract class :
virtual void log() const=0;

并且无法实例化。 Button类必须提供log方法的实现,或者它也不能实例化,并且必须标记为virtual,并且具有与基类定义完全相同的签名。换句话说,复制该行,删除= 0;部分,提供实现,它应该摆脱该错误消息。

and it can not be instantiated. The Button class must provide an implementation of the log method or it can not be instantiated either and it must be labelled as virtual and have the exact same signature as the base class' definition of it. In other words, copy that line, remove the "=0;" part, provide the implementation, and it should get rid of that error message.


#ifndef CONTROL_HPP_INCLUDED

#define CONTROL_HPP_INCLUDED

class控制{

受保护:

int id;

int width;

int height;

public:

Control(){}

Control(int i,int w,int h):id(i),width(w),height( h){}

控制(const控制& c):id(c.id),宽度(c.width),高度(c.height){}

virtual void log()const = 0;



int getId()const {

返回id;

}



int getWidth()const {

返回宽度;

}



int getHeight()const {

返回高度;

}



};



#endif // CONTROL_HPP_INCLUDED
#ifndef CONTROL_HPP_INCLUDED
#define CONTROL_HPP_INCLUDED
class Control{
protected:
int id;
int width;
int height;
public:
Control(){}
Control(int i,int w,int h):id(i),width(w),height(h){}
Control(const Control &c):id(c.id),width(c.width),height(c.height){}
virtual void log()const=0;

int getId() const {
return id;
}

int getWidth() const {
return width;
}

int getHeight() const {
return height;
}

};

#endif // CONTROL_HPP_INCLUDED


这篇关于塞尔维亚你好! :)当我尝试编译这个类时我有错误..问题是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆