一段简单的c++运算符重载的代码 各位帮忙看看错在哪里 我是没弄懂= =~新手 勿拍
#include<iostream.h>classcircle{private:doubleradius;public:circle();circle(doublex);...
#include<iostream.h>
class circle
{
private:
double radius;
public:
circle();
circle(double x);
friend int operator == (circle c1,circle c2);
friend int operator > (circle c1,circle c2);
friend void compare(circle c1,circle c2);
}
circle::circle()
{radius=(double)0;}
circle::circle(double x)
{radius=x;}
int operator == (circle c1,circle c2)
{
if (c1.radius==c2.radius)
return 1;
else
return 0;
}
int operator > (circle c1,circle c2)
{
if c1.radius>c2.radius
return 1;
else
return 0;
}
circle::compare(circle c1,circle c2)
{
if(c1==c2)
cout<<"c1,c2等大"<<
if(c1>c2)
cout<<"c1大于c2"<<
else
cout<<"c1小于c2"<<
}
void main()
{
circle c1(99);
circle c2(80);
compare(c1,c2);
}
编译显示的错误是:
[Error] C:\Users\fox\Documents\C-Free\Temp\未命名2.cpp:16: return type specification for constructor invalid
[Error] C:\Users\fox\Documents\C-Free\Temp\未命名2.cpp:31: parse error before `.'
[Error] C:\Users\fox\Documents\C-Free\Temp\未命名2.cpp:35: confused by earlier errors, bailing out 展开
class circle
{
private:
double radius;
public:
circle();
circle(double x);
friend int operator == (circle c1,circle c2);
friend int operator > (circle c1,circle c2);
friend void compare(circle c1,circle c2);
}
circle::circle()
{radius=(double)0;}
circle::circle(double x)
{radius=x;}
int operator == (circle c1,circle c2)
{
if (c1.radius==c2.radius)
return 1;
else
return 0;
}
int operator > (circle c1,circle c2)
{
if c1.radius>c2.radius
return 1;
else
return 0;
}
circle::compare(circle c1,circle c2)
{
if(c1==c2)
cout<<"c1,c2等大"<<
if(c1>c2)
cout<<"c1大于c2"<<
else
cout<<"c1小于c2"<<
}
void main()
{
circle c1(99);
circle c2(80);
compare(c1,c2);
}
编译显示的错误是:
[Error] C:\Users\fox\Documents\C-Free\Temp\未命名2.cpp:16: return type specification for constructor invalid
[Error] C:\Users\fox\Documents\C-Free\Temp\未命名2.cpp:31: parse error before `.'
[Error] C:\Users\fox\Documents\C-Free\Temp\未命名2.cpp:35: confused by earlier errors, bailing out 展开
展开全部
1、类的声明结束后,}后面要加分号
2、有几句代码后面没写分号
3、compare是友元函数,不是成员函数,不用加circle::
4、cout的最后不能是<<
#include<iostream.h>
class circle
{
private:
double radius;
public:
circle();
circle(double x);
friend int operator == (circle c1,circle c2);
friend int operator > (circle c1,circle c2);
friend void compare(circle c1,circle c2);
};
circle::circle()
{radius=(double)0;}
circle::circle(double x)
{radius=x;}
int operator == (circle c1,circle c2)
{
if (c1.radius==c2.radius)
return 1;
else
return 0;
}
int operator > (circle c1,circle c2)
{
if (c1.radius>c2.radius)
return 1;
else
return 0;
}
void compare(circle c1,circle c2)
{
if(c1==c2)
cout<<"c1,c2等大";
if(c1>c2)
cout<<"c1大于c2";
else
cout<<"c1小于c2";
}
void main()
{
circle c1(99);
circle c2(80);
compare(c1,c2);
}
2、有几句代码后面没写分号
3、compare是友元函数,不是成员函数,不用加circle::
4、cout的最后不能是<<
#include<iostream.h>
class circle
{
private:
double radius;
public:
circle();
circle(double x);
friend int operator == (circle c1,circle c2);
friend int operator > (circle c1,circle c2);
friend void compare(circle c1,circle c2);
};
circle::circle()
{radius=(double)0;}
circle::circle(double x)
{radius=x;}
int operator == (circle c1,circle c2)
{
if (c1.radius==c2.radius)
return 1;
else
return 0;
}
int operator > (circle c1,circle c2)
{
if (c1.radius>c2.radius)
return 1;
else
return 0;
}
void compare(circle c1,circle c2)
{
if(c1==c2)
cout<<"c1,c2等大";
if(c1>c2)
cout<<"c1大于c2";
else
cout<<"c1小于c2";
}
void main()
{
circle c1(99);
circle c2(80);
compare(c1,c2);
}
展开全部
#include<iostream.h>
class circle
{
private:
double radius;
public:
circle();
circle(double x);
friend int operator == (circle c1,circle c2);
friend int operator > (circle c1,circle c2);
friend void compare(circle c1,circle c2);
};//分号
circle::circle()
{radius=(double)0;}
circle::circle(double x)
{radius=x;}
int operator == (circle c1,circle c2)
{
if (c1.radius==c2.radius)
return 1;
else
return 0;
}
int operator > (circle c1,circle c2)
{
if (c1.radius>c2.radius)//前后括号
return 1;
else
return 0;
}
//circle:://不是成员函数,去掉类域名
void compare(circle c1,circle c2)
{
if(c1==c2)
cout<<"c1,c2等大"<<endl;//不完整,注意分号结束语句
if(c1>c2)
cout<<"c1大于c2"<<endl;//不完整,注意分号结束语句
else
cout<<"c1小于c2"<<endl;//不完整,注意分号结束语句
}
void main()
{
circle c1(99);
circle c2(80);
compare(c1,c2);
}
class circle
{
private:
double radius;
public:
circle();
circle(double x);
friend int operator == (circle c1,circle c2);
friend int operator > (circle c1,circle c2);
friend void compare(circle c1,circle c2);
};//分号
circle::circle()
{radius=(double)0;}
circle::circle(double x)
{radius=x;}
int operator == (circle c1,circle c2)
{
if (c1.radius==c2.radius)
return 1;
else
return 0;
}
int operator > (circle c1,circle c2)
{
if (c1.radius>c2.radius)//前后括号
return 1;
else
return 0;
}
//circle:://不是成员函数,去掉类域名
void compare(circle c1,circle c2)
{
if(c1==c2)
cout<<"c1,c2等大"<<endl;//不完整,注意分号结束语句
if(c1>c2)
cout<<"c1大于c2"<<endl;//不完整,注意分号结束语句
else
cout<<"c1小于c2"<<endl;//不完整,注意分号结束语句
}
void main()
{
circle c1(99);
circle c2(80);
compare(c1,c2);
}
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询