主题:救命呀!我实在不行
wengchaohui
[专家分:0] 发布于 2008-01-02 11:15:00
问题如下:
class box
{public:
box(double lengthvalue=1.0,double breadthvalue=1.0,double heightvalue=1.0);
double volume()const;
int comparevolume(const box&otherbox)const;
private:
double length;
double breadth;
double height;
}
box::box(double lengthvalue,double breadthvalue,double heightvalue)
{length=lengthvalue;
breadth=breadthvalue;
height=heightvalue;
}
double box::volume()const
{return length*breadth*height;
}
int box::comparevolume(const box&otherbox)const
{double vol1=this->volume();
double vol2=otherbox.volume();
return vol1>vol2 ? 1:(vol1<vol2 ?-1:0);
}
我怎么找也找不出错误,可是编译时总提示错误C:\Documents and Settings\Administrator\桌面\新建文件夹\420.cpp(14) : error C2533: 'box::box' : constructors not allowed a return type
各位大侠,帮小弟解决一下吧!
回复列表 (共5个回复)
沙发
stdlll [专家分:360] 发布于 2008-01-02 12:48:00
[code]
class box
{public:
box(double lengthvalue=1.0,double breadthvalue=1.0,double heightvalue=1.0);
double volume()const;
int comparevolume(const box&otherbox)const;
private:
double length;
double breadth;
double height;
}
[color=FF0000][size=6]; [/size] //这里加一个分号!!!!!!!![/color]
box::box(double lengthvalue,double breadthvalue,double heightvalue)
{length=lengthvalue;
breadth=breadthvalue;
height=heightvalue;
}
double box::volume()const
{return length*breadth*height;
}
int box::comparevolume(const box&otherbox)const
{double vol1=this->volume();
double vol2=otherbox.volume();
return vol1>vol2 ? 1:(vol1<vol2 ?-1:0);
}
[/code]
板凳
wengchaohui [专家分:0] 发布于 2008-01-02 13:53:00
还有一个问题
class box
{public:
box(double lengthvalue=1.0,double breadthvalue=1.0,double heightvalue=1.0);
double volume()const;
int comparevolume(const box&otherbox)const;//为什么最后的const 去掉也没关系,!!???
//在加个函数
double l();//声明,定义在下面
private:
double length;
double breadth;
double height;
};
box::box(double lengthvalue,double breadthvalue,double heightvalue)
{length=lengthvalue;
breadth=breadthvalue;
height=heightvalue;
}
double box::volume()const
{return length*breadth*height;
}
int box::comparevolume(const box&otherbox)const//为什么最后的const 去掉也没关,!!???
{double vol1=this->volume();
double vol2=otherbox.volume();
return vol1>vol2 ? 1:(vol1<vol2 ?-1:0);
}
double Box::l() //函数定义
{return length+breadth;
cout<<length+breadth;
}
int main()
{Box firstBox(17.0,11.0,3.0);
cout<<firstBox.comparevolume(firstBox)<<endl;
cout<<firstBox.volume();
firstBox.l(); //引用了它,为什么不会输出length+breadth的值
return 0;
}
所以问题是:在文中,const是作什么用的,小弟不明白:
问题2:
fsffs
3
561Press any key to continue
在主函数中firstBox.l(); //引用了它,为什么不会输出length+breadth的值
3 楼
stdlll [专家分:360] 发布于 2008-01-02 14:05:00
>>int comparevolume(const box&otherbox)const;//为什么最后的const 去掉也没关系,!!???
呵呵。你在main函数里定义一个 const Box secondBox(17.0,11.0,3.0);
secondBox.comparevolume(fistBox);// 试试!
这里const成员函数是说我这个成员函数是不会改变这个对象的值的,你(编译器你放心好了!)。
>>firstBox.l(); //引用了它,为什么不会输出length+breadth的值
这个:
>>double Box::l() //函数定义
>>{return length+breadth;
>>cout<<length+breadth;
>>}
你都先return了。这个函数在
return length+breadth;的时候就结束了,没有机会执行到cout<<length+breadth;不是吗?
4 楼
wengchaohui [专家分:0] 发布于 2008-01-02 14:55:00
//我改了一下,可是直接拷过去试一下,真是麻烦各位了
#include <iostream.h>
class Box
{public:
Box(double lengthvalue=1.0,double breadthvalue=1.0,double heightvalue=1.0);
double volume()const;
int comparevolume(const Box&otherBox)const; //为什么最后的const 去掉也没关
double l(); //就是它害我 声明
private:
double length;
double breadth;
double height;
};
Box::Box(double lengthvalue,double breadthvalue,double heightvalue): length(lengthvalue),
breadth(breadthvalue),
height(heightvalue)
{cout <<"fsffs" <<endl;}
double Box::volume()const
{return length*breadth*height;}
int Box::comparevolume(const Box&otherBox)const //为什么最后的const 去掉也没关
{double vol1=this-> volume();
double vol2=otherBox.volume();
return vol1> vol2 ? 1:(vol1 <vol2 ?-1:0);}
double Box::l() //double l()函数定义
{return length+breadth;
cout <<"length+breadth" <<length+breadth ;} //里面明明有输出
int main()
{Box firstBox(17.0,11.0,3.0);
cout <<firstBox.comparevolume(firstBox) <<endl;
cout <<firstBox.volume();
firstBox.l();
return 0;
} /*
问题1:文中const好像作用不明显:
问题2:
结果如下
fsffs
3
561Press any key to continue 真的请你们帮忙,*/
谢谢,根据您的提示,第二个问题解决了,第一个吗!!呵呵,被您说中了,提示错误如下:
error C2662: 'comparevolume' : cannot convert 'this' pointer from 'const class Box' to 'class Box &'
我思考虑一下,,
5 楼
wengchaohui [专家分:0] 发布于 2008-01-02 15:29:00
哦 我知道了,好像const对象只能访问const 函数
我来回复