c++重载<<运算符
voidmain(){boolfuck=true;ostream&operator<<(ostream&s,fp){s<<p.s<<"/"<<p.m<<endl;retu...
void main(){
bool fuck = true;
ostream &operator << (ostream &s, f p){
s << p.s << "/" << p.m << endl;
return s;
}
cout << "欢迎使用老赵分数计算器,仅供测试,登不得大雅!"<<endl;
while (fuck){
fuck = false;
char temp;
menu();
cout << "是否再次计算?Y/N:";
cin >> temp;
if (temp == 'y' || temp == 'Y')
fuck = true;
}
}
错误提示是:
1>d:\360data\重要数据\我的文档\visual studio 2013\projects\fraction\fraction\源.cpp(92): error C2601: “operator <<”: 本地函数定义是非法的
1> d:\360data\重要数据\我的文档\visual studio 2013\projects\fraction\fraction\源.cpp(90): 此行有一个“{”没有匹配项 展开
bool fuck = true;
ostream &operator << (ostream &s, f p){
s << p.s << "/" << p.m << endl;
return s;
}
cout << "欢迎使用老赵分数计算器,仅供测试,登不得大雅!"<<endl;
while (fuck){
fuck = false;
char temp;
menu();
cout << "是否再次计算?Y/N:";
cin >> temp;
if (temp == 'y' || temp == 'Y')
fuck = true;
}
}
错误提示是:
1>d:\360data\重要数据\我的文档\visual studio 2013\projects\fraction\fraction\源.cpp(92): error C2601: “operator <<”: 本地函数定义是非法的
1> d:\360data\重要数据\我的文档\visual studio 2013\projects\fraction\fraction\源.cpp(90): 此行有一个“{”没有匹配项 展开
2个回答
展开全部
说起来比较麻烦,还是自己看起来领悟的快。给你个简单的运算符重载的例子。重载了两个 点的加减运算。他使本来只可以在数值上用+,- 运算的可以被坐标点运用。点X(1,1)+Y(2,2)=Z(3,3).例子如下,成功调试。
#include <iostream>
using namespace std;
class Lyhdian
{
double X;
double Y;
public:
Lyhdian(double,double);
Lyhdian operator +(Lyhdian&);
Lyhdian operator -(Lyhdian&);
void show();
};
Lyhdian::Lyhdian(double a,double b)
{
X=a;
Y=b;
}
Lyhdian Lyhdian::operator +(Lyhdian& a1)
{
double c=X+a1.X;
double d=Y+a1.Y;
Lyhdian result(c,d);
return result;
}
Lyhdian Lyhdian::operator -(Lyhdian& a1)
{
double c=X-a1.X;
double d=Y-a1.Y;
Lyhdian result(c,d);
return result;
}
void Lyhdian::show()
{
cout<<"点的坐标为:"<<endl;
cout<<"("<<X<<","<<Y<<")"<<endl;
}
void main()
{
double X;
double Y;
Lyhdian cc(0,0);
cout<<"请输入一点的坐标:"<<endl;
cin>>X>>Y;
Lyhdian aa(X,Y);
cout<<"请输入另一点的坐标:"<<endl;
cin>>X>>Y;
Lyhdian bb(X,Y);
cout<<"两点进行加运算:"<<endl;
cc=aa+bb;
cc.show();
cout<<"两点进行减运算:"<<endl;
cc=aa-bb;
cc.show();
}
#include <iostream>
using namespace std;
class Lyhdian
{
double X;
double Y;
public:
Lyhdian(double,double);
Lyhdian operator +(Lyhdian&);
Lyhdian operator -(Lyhdian&);
void show();
};
Lyhdian::Lyhdian(double a,double b)
{
X=a;
Y=b;
}
Lyhdian Lyhdian::operator +(Lyhdian& a1)
{
double c=X+a1.X;
double d=Y+a1.Y;
Lyhdian result(c,d);
return result;
}
Lyhdian Lyhdian::operator -(Lyhdian& a1)
{
double c=X-a1.X;
double d=Y-a1.Y;
Lyhdian result(c,d);
return result;
}
void Lyhdian::show()
{
cout<<"点的坐标为:"<<endl;
cout<<"("<<X<<","<<Y<<")"<<endl;
}
void main()
{
double X;
double Y;
Lyhdian cc(0,0);
cout<<"请输入一点的坐标:"<<endl;
cin>>X>>Y;
Lyhdian aa(X,Y);
cout<<"请输入另一点的坐标:"<<endl;
cin>>X>>Y;
Lyhdian bb(X,Y);
cout<<"两点进行加运算:"<<endl;
cc=aa+bb;
cc.show();
cout<<"两点进行减运算:"<<endl;
cc=aa-bb;
cc.show();
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询