关于C++中类的冒泡排序,实在想不出了,高手指教下~!!

来源:百度知道 编辑:UC知道 时间:2024/06/02 20:08:54
sale_product sp[100];
//前面类的内容我就不写了,系统说太长,sale_product是个类,我想根据赢利的大小来冒泡排序显示,但老是错误,不知道错那里~谁帮我解决下~
int t;

for(i=0;i<n;i++)
{
for(int j=0;j<i;j++)
{
if(sp[j].yingli()>sp[j+1].yingli())
{
t=sp[j];
sp[j]=sp[j+1];
sp[j+1]=t;
}
}
}
'=' : no operator defined which takes a right-hand operand of type 'int' (or there is no acceptable conversion)
E:\iii.cpp(962) : error C2679: binary '=' : no operator defined which takes a right-hand operand of type 'int' (or there is no acceptable conversion)
这是错误提示,循环应该没错,错误的地方应该是"="号那里,我不清楚类是否能用等号连接~

循环的问题看楼上说的
错误的原因是你自定义的类sale_product没有对“=”重载
相关详细内容可以上网查或者看课本里关于运算符重载部分
举例
sale_product& sale_product::operator=(const sale_product &A)
{
//...将A中成员 赋给 调用的对象 的相应成员
return *this;
}

for(i=0;i<n;i++)
{
for(int j=0;j<n-i;j++) //改成这样你看看对不?
{
if(sp[j].yingli()>sp[j+1].yingli())
{
t=sp[j];
sp[j]=sp[j+1];
sp[j+1]=t;
}
}
}

说明一下错误的提示信息啊。
另:两个循环中的里层循环写错了:
应该是:for(int j=0;j<n-i;j++)

两个循环中的里层循环写错了:
应该是:for(int j=0;j<n-i;j++)

给你一个标准的冒泡排序:
for( times = length - 1; times > 0; times--) //排序的次数
for( client = 0; client < times; client++) //控制比较
if (item[client] > item[client + 1]) //比较交换
{
//交换client和client + 1的值
}
你参考下