C++编译错误:'operator <<' is ambiguous
向派生类对象用重载的“<<”,编译是提示错误:'operator<<'isambiguous。————————————#include<iostream.h>classB...
向派生类对象用重载的“<<”,编译是提示错误:'operator <<' is ambiguous。
————————————
#include<iostream.h>
class Building
{
protected:
int cs;
public:
Building(int cs=0);
int getcs();
};
Building::Building(int a)
{
cs=a;
}
int Building::getcs() {return cs;}
class Office:public Building
{
protected:
int mhqs;
public:
Office(int cs=0, int mhqs=0);
int getmhqs();
friend ostream & operator << (ostream &, Office &);
};
Office::Office(int a, int f):Building(a),mhqs(f){}
int Office::getmhqs(){return mhqs;}
ostream & operator << (ostream & output, Office o)
{
output<<"Office:"<<endl;
output<<"层数:"<<o.getcs()<<endl;
output<<"灭火器数:"<<o.getmhqs()<<endl;
return output;
}
void main()
{
Office cO(10,25);
cout<<cO;
————————————
错误提示:
Compiling...
cpp_05c_class.cpp
F:\sth\cpp05\c2\cpp_05c_class.cpp(46) : error C2593: 'operator <<' is ambiguous
执行 cl.exe 时出错.
cpp_05c_class.obj - 1 error(s), 0 warning(s) 展开
————————————
#include<iostream.h>
class Building
{
protected:
int cs;
public:
Building(int cs=0);
int getcs();
};
Building::Building(int a)
{
cs=a;
}
int Building::getcs() {return cs;}
class Office:public Building
{
protected:
int mhqs;
public:
Office(int cs=0, int mhqs=0);
int getmhqs();
friend ostream & operator << (ostream &, Office &);
};
Office::Office(int a, int f):Building(a),mhqs(f){}
int Office::getmhqs(){return mhqs;}
ostream & operator << (ostream & output, Office o)
{
output<<"Office:"<<endl;
output<<"层数:"<<o.getcs()<<endl;
output<<"灭火器数:"<<o.getmhqs()<<endl;
return output;
}
void main()
{
Office cO(10,25);
cout<<cO;
————————————
错误提示:
Compiling...
cpp_05c_class.cpp
F:\sth\cpp05\c2\cpp_05c_class.cpp(46) : error C2593: 'operator <<' is ambiguous
执行 cl.exe 时出错.
cpp_05c_class.obj - 1 error(s), 0 warning(s) 展开
1个回答
展开全部
把下面的友元放到类定义里面去
friend ostream & operator << (ostream & output, Office o)
{
output<<"Office:"<<endl;
output<<"层数:"<<o.getcs()<<endl;
output<<"灭火器数:"<<o.getmhqs()<<endl;
return output;
}
friend ostream & operator << (ostream & output, Office o)
{
output<<"Office:"<<endl;
output<<"层数:"<<o.getcs()<<endl;
output<<"灭火器数:"<<o.getmhqs()<<endl;
return output;
}
本回答被提问者和网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询