C++类继承问题

发布于 2024-08-11 01:36:06 字数 1316 浏览 4 评论 0原文

您好,我有两个类,一个称为指令,一个称为 LDI,它继承自指令类。

class Instruction{

  protected:
    string name;
    int value;

  public:
    Instruction(string _name, int _value){ //constructor
        name = _name;
        value = _value;
    }
    ~Instruction(){}
    Instruction (const Instruction &rhs){
        name = rhs.name;
        value = rhs.value;
    }
    void setName(string _name){
        name = _name;
    }
    void setValue(int _value){
        value = _value;
    }
    string getName(){
        return name;
    }
    int getValue(){
        return value;
    }
    virtual void execute(){}
    virtual Instruction* Clone() { 
        return new Instruction(*this); 
    }
};
/////////////end of instruction super class //////////////////////////

class LDI : public Instruction{

    void execute(){
        //not implemented yet
    }
    virtual Instruction* Clone(){
        return new LDI(*this);
    }
};

然后,我创建一个指令类型的指针,并尝试指向 LDI 类型的新实例。

Instruction* ptr;
ptr = new LDI("test", 22);

我收到以下编译器错误。有什么想法我做错了吗?

functions.h:71: error: no matching function for call to ‘LDI::LDI(std::string&, int&)’
classes.h:54: note: candidates are: LDI::LDI()
classes.h:54: note:                 LDI::LDI(const LDI&)

Hi I have two classes, one called Instruction, one called LDI which inherits from instruction class.

class Instruction{

  protected:
    string name;
    int value;

  public:
    Instruction(string _name, int _value){ //constructor
        name = _name;
        value = _value;
    }
    ~Instruction(){}
    Instruction (const Instruction &rhs){
        name = rhs.name;
        value = rhs.value;
    }
    void setName(string _name){
        name = _name;
    }
    void setValue(int _value){
        value = _value;
    }
    string getName(){
        return name;
    }
    int getValue(){
        return value;
    }
    virtual void execute(){}
    virtual Instruction* Clone() { 
        return new Instruction(*this); 
    }
};
/////////////end of instruction super class //////////////////////////

class LDI : public Instruction{

    void execute(){
        //not implemented yet
    }
    virtual Instruction* Clone(){
        return new LDI(*this);
    }
};

Then I create a pointer of type Instruction and try to make point to a new instance of type LDI.

Instruction* ptr;
ptr = new LDI("test", 22);

I get the following compiler errors. Any ideas what I'm doing wrong?

functions.h:71: error: no matching function for call to ‘LDI::LDI(std::string&, int&)’
classes.h:54: note: candidates are: LDI::LDI()
classes.h:54: note:                 LDI::LDI(const LDI&)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

玩物 2024-08-18 01:36:06

代码:new LDI(name, val) 特别指出“使用 nameval 调用 LDI 构造函数。”

没有采用 name / val 的 LDI 构造函数。
事实上,我根本没有看到 LDI 的构造函数。

如果您想使用基类的构造函数,请按以下步骤操作:

public LDI(string _name, int _value) // Public constructor for LDI
    : Instruction(_name, _value)     // Delegate to the base-class constructor
{
    // Do more LDI-specific construction here
}

The code: new LDI(name, val) specifically says "Call the LDI constructor with a name and val."

There is no LDI constructor that takes name / val.
In fact, I don't see a constructor for LDI at all.

If you want to use the constructor of a base-class, here is how:

public LDI(string _name, int _value) // Public constructor for LDI
    : Instruction(_name, _value)     // Delegate to the base-class constructor
{
    // Do more LDI-specific construction here
}
爱*していゐ 2024-08-18 01:36:06
LDI::LDI (string _name, int _value):Instruction(_name,_value){}

您需要为派生类LDI提供构造函数,该构造函数又调用正确的基类构造函数。

ptr = new LDI("test", 22);

此时编译器会寻找一个以(string,int)作为参数的LDI构造函数,因为编译器cribs没有提供这样的构造函数。

LDI(string _name, int _value)
{ 
}

通过提供派生类构造函数将解决编译问题。但默认情况下,派生类构造函数不会调用适当的基类构造函数;在本例中 Instruction(_name,_value)(它仅调用默认构造函数)。
为了调用正确的基类构造函数,您需要从派生类初始值设定项列表中调用基类构造函数。

所以。

LDI::LDI (string _name, int _value):Instruction(_name,_value){}
LDI::LDI (string _name, int _value):Instruction(_name,_value){}

You need to provide a constructor for derived class LDI, which in turn calls the correct Base class constructor.

ptr = new LDI("test", 22);

At this moment compiler looks for a LDI constructor which takes (string,int) as arguments, Since there is no such constructor provided the compiler cribs.

LDI(string _name, int _value)
{ 
}

By providing the derived class constructor will solve the compilation issue. But by default the derived class constructor will not call appropriate base class constructor; in this case Instruction(_name,_value)( it calls only the default constructor).
In order to call correct base class constructor you need to invoke the base class constructor from derived class initializer list.

so.

LDI::LDI (string _name, int _value):Instruction(_name,_value){}
廻憶裏菂餘溫 2024-08-18 01:36:06

基类的构造函数、析构函数、赋值运算符、友元函数和基类的友元类不能被派生类继承。

Constructors, destructors, assignment operator, friend functions and friend classes of base classes are not inherited by derived classes.

何时共饮酒 2024-08-18 01:36:06

首先,您必须定义并声明 LDI 的构造函数(string _name,int _value)。您还可以初始化基类构造函数 LDI::LDI (string _name, int _value):Instruction(_name,_value){}。
其次,如果在基类析构函数之前添加 vritual 关键字会很好。如果你的基类析构函数不是虚拟的并且你编写了这段代码
指令* ptr;
ptr = 新 LDI("测试", 22);
删除*ptr;

LDI 的析构函数从未被调用。保持基类析构函数为虚拟,以便正确销毁对象层次结构

Firs of all you must define and declare constructor for LDI (string _name, int _value). You myst initialize your base class constructor also LDI::LDI (string _name, int _value):Instruction(_name,_value){}.
Second it will be good if add vritual keyword before your base class destructor. If your base class destructor not virtual and you write this code
Instruction* ptr;
ptr = new LDI("test", 22);
delete *ptr;

destructor for LDI never called. Keep base class destructor virtual for correct destroy for your hierarchy of objects

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文