【发布时间】:2016-10-08 05:00:29
【问题描述】:
我在 DEV c++ 中的 c++ 中有以下场景
class base{
int i=0;
public:
base(){
cout<<"base default constructor"<<endl;
}
base(int i){
cout<<"base with int"<<endl;
}
};
class derive : public base{
int j;
public:
derive(){
cout<<"derive default";
}
derive(int i){
int k=5;
base(k);//////error
cout<<"derived with int "<<i<<endl;
}
void fun(int i){
cout<<"finction "<<i<<endl;
}
};
int main()
{
derive d(9);
}
运行后出现以下错误
[错误] 声明“base k”冲突
[错误]“k”之前的声明为“int k”
为什么会出现此错误。
当我使用派生构造函数中声明的任何参数调用 base(5) 时,它工作正常。
提前致谢。
【问题讨论】:
-
"DEV c++" 不是编译器。
-
嗨,是的,它是临时的,我没有初始化构造函数。但我想知道为什么 base(5) 或 base(j) 在 derived() 中有效,但在 base(i) 中无效。
标签: c++ inheritance constructor visibility derived-class