error C2297: '<<' : illegal, right operand has type 'char [20]' 父类静态变量怎么使用,这错误怎么改
#include"iostream"usingnamespacestd;classbase0{public:base0(inta,intb):a(a),v0(b){cou...
#include"iostream"
using namespace std;
class base0
{
public:
base0(int a,int b):a(a),v0(b)
{
cout<<"base0构造函数被调用"<<endl;
}
base0()
{
cout<<"base0无参构造函数被调用"<<endl;
}
~base0()
{
cout<<"base0析造函数被调用"<<endl;
}
base0(base0& b):a(b.a)
{
cout<<"base0复制构造函数被调用"<<endl;
}
void fun()
{
cout<<"call base0"<<endl;
}
int v0;
static int cout;
private:
int a;
};
int base0::cout=0;
class base1: public virtual base0
{
public:
void getcout()
{
cout<<"cout="<<cout<<endl;
}
base1(int b,int c):b(b),base0(c,3) //error C2512: 'base0' : no appropriate default constructor available
{
cout<<"base1构造函数被调用"<<endl;
}
base1()
{
cout<<"base1无参构造函数被调用"<<endl;
}
~base1()
{
cout<<"base1析造函数被调用"<<endl;
}
base1(base1& b):b(b.b),base0(b)
{
cout<<"base1复制构造函数被调用"<<endl;
}
void fun()
{
cout<<"call base1"<<endl;
}
void fun(int)
{
cout<<"call y base1"<<endl;
}
private:
int b;
};
/*class base3: public virtual base0
{
public:
base3(int b,int c):b(b),base0(c,4) //error C2512: 'base0' : no appropriate default constructor available
{
cout<<"base3构造函数被调用"<<endl;
}
base3()
{
cout<<"base3无参构造函数被调用"<<endl;
}
~base3()
{
cout<<"base3析造函数被调用"<<endl;
}
base3(base3& b):b(b.b),base0(b)
{
cout<<"base3复制构造函数被调用"<<endl;
}
void fun()
{
cout<<"call base3"<<endl;
}
private:
int b;
};
*/
class base2:public base1//,public base3
{
public:
using base1::fun;
base2(int c,int a,int d,int e,int f,int g):a(c,a),base1(d,e)//,base3(f,g)
{
cout<<"base2构造函数被调用"<<endl;
}
base2()
{
cout<<"base2无参构造函数被调用"<<endl;
}
~base2()
{
cout<<"base2析造函数被调用"<<endl;
}
base2(base2 & b):base1(b)
{
cout<<"base2复制构造函数被调用"<<endl;
}
void fun()
{
cout<<"call base2"<<endl;
}
private:
base0 a;
base1 b;
};
int main(int argc, _TCHAR* argv[])
{
base2 m(4,3,2,1,6,7);
base2 n(m);
m.fun(2);// 同名但参数不同
m.fun();//同名但覆盖
cout<<"v0="<<m.base1::v0<<endl;
m.getcout();
cout<<"v0="<<m.v0<<endl;
return 0;
} 展开
using namespace std;
class base0
{
public:
base0(int a,int b):a(a),v0(b)
{
cout<<"base0构造函数被调用"<<endl;
}
base0()
{
cout<<"base0无参构造函数被调用"<<endl;
}
~base0()
{
cout<<"base0析造函数被调用"<<endl;
}
base0(base0& b):a(b.a)
{
cout<<"base0复制构造函数被调用"<<endl;
}
void fun()
{
cout<<"call base0"<<endl;
}
int v0;
static int cout;
private:
int a;
};
int base0::cout=0;
class base1: public virtual base0
{
public:
void getcout()
{
cout<<"cout="<<cout<<endl;
}
base1(int b,int c):b(b),base0(c,3) //error C2512: 'base0' : no appropriate default constructor available
{
cout<<"base1构造函数被调用"<<endl;
}
base1()
{
cout<<"base1无参构造函数被调用"<<endl;
}
~base1()
{
cout<<"base1析造函数被调用"<<endl;
}
base1(base1& b):b(b.b),base0(b)
{
cout<<"base1复制构造函数被调用"<<endl;
}
void fun()
{
cout<<"call base1"<<endl;
}
void fun(int)
{
cout<<"call y base1"<<endl;
}
private:
int b;
};
/*class base3: public virtual base0
{
public:
base3(int b,int c):b(b),base0(c,4) //error C2512: 'base0' : no appropriate default constructor available
{
cout<<"base3构造函数被调用"<<endl;
}
base3()
{
cout<<"base3无参构造函数被调用"<<endl;
}
~base3()
{
cout<<"base3析造函数被调用"<<endl;
}
base3(base3& b):b(b.b),base0(b)
{
cout<<"base3复制构造函数被调用"<<endl;
}
void fun()
{
cout<<"call base3"<<endl;
}
private:
int b;
};
*/
class base2:public base1//,public base3
{
public:
using base1::fun;
base2(int c,int a,int d,int e,int f,int g):a(c,a),base1(d,e)//,base3(f,g)
{
cout<<"base2构造函数被调用"<<endl;
}
base2()
{
cout<<"base2无参构造函数被调用"<<endl;
}
~base2()
{
cout<<"base2析造函数被调用"<<endl;
}
base2(base2 & b):base1(b)
{
cout<<"base2复制构造函数被调用"<<endl;
}
void fun()
{
cout<<"call base2"<<endl;
}
private:
base0 a;
base1 b;
};
int main(int argc, _TCHAR* argv[])
{
base2 m(4,3,2,1,6,7);
base2 n(m);
m.fun(2);// 同名但参数不同
m.fun();//同名但覆盖
cout<<"v0="<<m.base1::v0<<endl;
m.getcout();
cout<<"v0="<<m.v0<<endl;
return 0;
} 展开
1个回答
展开全部
追问
我用的是vc,如果去了static int cout,编译就能通过啊
追答
你这一说才想起,是你定义的static int cout;变量名cout与std::count冲突了
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询