C++类继承问题
您好,我有两个类,一个称为指令,一个称为 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
代码:
new LDI(name, val)
特别指出“使用name
和val
调用 LDI 构造函数。”没有采用
name / val
的 LDI 构造函数。事实上,我根本没有看到 LDI 的构造函数。
如果您想使用基类的构造函数,请按以下步骤操作:
The code:
new LDI(name, val)
specifically says "Call the LDI constructor with aname
andval
."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:
您需要为派生类
LDI
提供构造函数,该构造函数又调用正确的基类构造函数。此时编译器会寻找一个以
(string,int)
作为参数的LDI
构造函数,因为编译器cribs没有提供这样的构造函数。通过提供派生类构造函数将解决编译问题。但默认情况下,派生类构造函数不会调用适当的基类构造函数;在本例中
Instruction(_name,_value)
(它仅调用默认构造函数)。为了调用正确的基类构造函数,您需要从派生类初始值设定项列表中调用基类构造函数。
所以。
You need to provide a constructor for derived class
LDI
, which in turn calls the correct Base class constructor.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.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.
基类的构造函数、析构函数、赋值运算符、友元函数和基类的友元类不能被派生类继承。
Constructors, destructors, assignment operator, friend functions and friend classes of base classes are not inherited by derived classes.
首先,您必须定义并声明 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