【发布时间】:2013-12-03 23:35:34
【问题描述】:
我正在尝试编译必须使用 C++ 继承的旧代码。我已经修复了一段时间,但我放弃了这个错误:
class Operation{
public:
char *op;
//Operation();
Operation(char op[]){
(*this).op = op;
}
};
class Param : Operation{
public:
int value;
Param(char op[], int value) : Operation(op){
(*this).op=op;
(*this).value=value;
}
int getValue(){
return this->value;
}
};
class MyBase{
public:
int valuea;
int valueb;
Operation operation;
MyBase(int a, int b, Operation* op){
valuea=a;
valueb=b;
operation=*op;
}
Operation* getSum(){};
Operation* getRest(){};
};
class MyExtend : public MyBase{
public:
Param* getSum(){
Param pam=new Param(this->valuea+this->valueb,"add");
return pam;
}
Param* getRest(){
Param pam=new Param(this->valuea-this->valueb,"rest");
return pam;
}
};
class Exec{
public int static main(args[] params){
MyExtend ext=new MyExtend(6,4);
cout << ext.getSum().getValue() << endl;
cout << ext.getRest().getValue() << endl;
return 0;
}
};
我正在关注
的错误MyBase(int a, int b, Operation* op){
即:
错误:没有匹配的函数调用'Operation::Operation()'
我很惊讶,因为它看起来像是在检查 Operation 的默认构造函数。如果我重载默认构造函数,它可以工作,但我不明白为什么。如果我通过值或引用传递操作并不重要。你能给我一些关于这个功能的提示吗?我无法找到一致的回应。请忽略以下错误,因为我正在处理它们。
一切顺利。
【问题讨论】:
标签: c++ constructor parameter-passing