English 中文(简体)
C++ Class Inheritance problem
原标题:

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&)
最佳回答

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
}
问题回答
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){}

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

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





相关问题
Undefined reference

I m getting this linker error. I know a way around it, but it s bugging me because another part of the project s linking fine and it s designed almost identically. First, I have namespace LCD. Then I ...

C++ Equivalent of Tidy

Is there an equivalent to tidy for HTML code for C++? I have searched on the internet, but I find nothing but C++ wrappers for tidy, etc... I think the keyword tidy is what has me hung up. I am ...

Template Classes in C++ ... a required skill set?

I m new to C++ and am wondering how much time I should invest in learning how to implement template classes. Are they widely used in industry, or is this something I should move through quickly?

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

typedef ing STL wstring

Why is it when i do the following i get errors when relating to with wchar_t? namespace Foo { typedef std::wstring String; } Now i declare all my strings as Foo::String through out the program, ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

Window iconification status via Xlib

Is it possible to check with the means of pure X11/Xlib only whether the given window is iconified/minimized, and, if it is, how?

热门标签