我的程序为什么运行出错啊!(operator has no effect; expected operator with side-effect)

wonhomple 2009-04-09 12:42:24
class shape
{
public:
virtual float area()=0;
};
class rectangle:public shape
{
private:
float H,W;
public:
float area()
{return H*W;}
rectangle(float h,float w)
{
H=h,W=w;
}
};
class circle:public shape
{
private:
float R;
public:
circle(float r)
{
R=r;
}
float area()
{
return 3.14 *R*R;
}
};
#include <iostream.h>
#include "area.h"
void sort(shape *a[],int n)
{
shape *ptr;
for (int i=0;i<n;i++)
for(int j=1;j<=n;j++)
{
a[i]->area() > a[j]->area();
ptr=a[i];
a[i]=a[j];
a[j]=ptr;
}
}
float total(shape *a[],int n)
{
float sum=0.0;
for(int i=0;i<n;i++)
{
sum+=a[i]->area();
}
return sum;
}
void show(shape *a[],int n)
{
for(int i=0;i<n;i++)

cout<<"the arear of the shape"<<a[i]->area()<<endl;

}
int main()
{
float sum;
circle C1(3.0),C2(2.0);
rectangle R1(2.0,1.0),R2(1.0,3.0);
shape *sp[4];
sp[0]=&C1;sp[1]=&C2;
sp[2]=&R1;sp[3]=&R2;
sort(sp,4);
show(sp,4);
sum=total(sp,4);
cout<<"the total area is :"<<sum<<endl;
return 0;

}
问题是在红题字部分总是出现operator has no effect; expected operator with side-effect的警告信息!高手帮忙啊!

...全文
1895 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
sunyard_dj 2011-10-20
  • 打赏
  • 举报
回复
看都没看明白都说懂了~哎。。。
  • 打赏
  • 举报
回复
a[i]->area() > a[j]->area();
晕,这样肯定没有意义啊。

那你在代码里写个
1;
你觉得编译器会通过吗?
如果是要判断,就加if
黄志义 2009-04-09
  • 打赏
  • 举报
回复
UP
[Quote=引用 1 楼 lzr4304061988012 的回复:]
C/C++ code
语法没错;
a[i]->area() > a[j]->area();
你这个比较大小没什么意义,你是要判断吗
if(a[i]->area() > a[j]->area() )
[/Quote]
lzr4304061988012 2009-04-09
  • 打赏
  • 举报
回复

语法没错;
a[i]->area() > a[j]->area();
你这个比较大小没什么意义,你是要判断吗
if(a[i]->area() > a[j]->area() )

Dinelgua 2009-04-09
  • 打赏
  • 举报
回复
不谢
wonhomple 2009-04-09
  • 打赏
  • 举报
回复
哦!对了,忘记了!多谢各位

65,210

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧