我是刚刚近来的,看一看你们的智慧吧

gangzichh 2005-11-14 10:36:47
#include<iostream.h>
class Box
{
public:
Box(int=10,int=10,int=10);
int volume();
private:
int height;
int width;
int length;
};
Box::Box(int h,int w,int len)
{
height=h;
width=w;
length=len;
}
int Box::volume()
{
return(height*width*length);
}

int main()
{
Box box1(15,30,25),Box2;
cout<<"The volume of box is"<<box1.volume<<endl;
box2=box1;
cout<<"The volume of box is"<<box2.volume<<endl;
return 0;
}
这是原代码呀!但是我不知道什么地方错呀!希望你们能解决一下吧
...全文
324 17 打赏 收藏 转发到动态 举报
写回复
用AI写文章
17 条回复
切换为时间正序
请发表友善的回复…
发表回复
罗嘉贇 2005-11-18
  • 打赏
  • 举报
回复
#include<iostream>
using namespace std;

class Box
{
public:
Box(int=10,int=10,int=10);
int volume();
private:
int height;
int width;
int length;
};

Box::Box(int h,int w,int len)
{
height=h;
width=w;
length=len;
}

int Box::volume()
{
return(height*width*length);
}

int main()
{
Box box1(15,30,25);
cout<<"The volume of box is"<<box1.volume()<<endl;
Box box2=box1;//如果要直接用 box2=box1;最好先重载=
cout<<"The volume of box is"<<box2.volume()<<endl;
return 0;
}
yulin001122 2005-11-18
  • 打赏
  • 举报
回复
默认的构造函数哪里去了?? Box box1(15,30,25),box2;
这句错了。。 box2调用默认的构造函数但因为你自己写了构造函数,所以默认的构造函数没有了。 自己再加一个没有参数的构造函数吧 或者
Box box1(15,30,25);
Box box2=box1;
EnginePlus 2005-11-18
  • 打赏
  • 举报
回复
???搞什么啊 大小写有区分的啊

youzelin 2005-11-16
  • 打赏
  • 举报
回复
这是谭浩强书上的吧,好像见过
huangyi101 2005-11-16
  • 打赏
  • 举报
回复
#include<iostream.h>
class Box
{
public:
Box(int=10,int=10,int=10);
int volume();
private:
int height;
int width;
int length;
};
Box::Box(int h,int w,int len)
{
height=h;
width=w;
length=len;
}
int Box::volume()
{
return(height*width*length);
}

int main()
{
Box box1(15,30,25),box2;
cout<<"The volume of box is"<<box1.volume()<<endl;
//box2=box1;
cout<<"The volume of box is"<<box2.volume()<<endl;
return 0;
}
dalish200 2005-11-15
  • 打赏
  • 举报
回复
Box box1(15,30,25),Box2;//这里定义Box2
cout<<"The volume of box is"<<box1.volume<<endl;//这里的volume没有加()
box2=box1;//这里用的box2
//楼主是粗心大王

glede0924 2005-11-15
  • 打赏
  • 举报
回复
#include<iostream.h>
class Box
{
public:
Box(int=10,int=10,int=10);
int volume();
private:
int height;
int width;
int length;
};
Box::Box(int h,int w,int len)
{
height=h;
width=w;
length=len;
}
int Box::volume()
{
return(height*width*length);
}

int main()
{
Box box1(15,30,25),box2;
cout<<"The volume of box is"<<box1.volume()<<endl;
box2=box1;
cout<<"The volume of box is"<<box2.volume()<<endl;
return 0;
}
superioi 2005-11-15
  • 打赏
  • 举报
回复
缺少默认的构造函数吧
glede0924 2005-11-15
  • 打赏
  • 举报
回复
#include<iostream.h>
class Box
{
public:
Box(int, int,int);
Box();
Box(Box &b);
int volume();
private:
int height;
int width;
int length;
};

Box::Box(int h,int w,int len)
{
height=h;
width=w;
length=len;
}

Box::Box()
{
height = 1;
length = 1;
width = 1;
}

Box::Box(Box &b)
{
b.height = height;
b.length = length;
b.width = width;
}
int Box::volume()
{
return (height*width*length);
}

int main()
{
Box box1(2,3,4), box2;
cout<<"The volume of box is "<<box1.volume()<<endl;
box2 = box1;
cout<<"The volume of box is "<<box2.volume()<<endl;
return 0;
}
conglingkaishi 2005-11-15
  • 打赏
  • 举报
回复
呵呵?考智慧?
gangzichh 2005-11-15
  • 打赏
  • 举报
回复
什么呀!这个好象是不行的呀!就没有一个狠的人吗?
dalish200 2005-11-14
  • 打赏
  • 举报
回复
1. box1.volume()&&box2.volume()没加()
2. 初始化对象时box2=box1前面要加上类名
dalish200 2005-11-14
  • 打赏
  • 举报
回复
#include<iostream.h>
class Box
{
public:
Box(int=10,int=10,int=10);
int volume();
private:
int height;
int width;
int length;
};
Box::Box(int h,int w,int len)
{
height=h;
width=w;
length=len;
}
int Box::volume()
{
return(height*width*length);
}

int main()
{
Box box1(15,30,25),Box2;
cout<<"The volume of box is"<<box1.volume()<<endl;
Box box2(box1);
cout<<"The volume of box is"<<box2.volume()<<endl;
return 0;
}
youruhe 2005-11-14
  • 打赏
  • 举报
回复
你太粗心了!
别的不看
Box box1(15,30,25),Box2;
cout<<"The volume of box is"<<box1.volume<<endl;
box2=box1;
cout<<"The volume of box is"<<box2.volume<<endl;
return 0;
前面定义Box2,后面怎么又是box2? && volume方法也不加()
lance_123 2005-11-14
  • 打赏
  • 举报
回复
得写一个=的重载函数了.
sasdaa 2005-11-14
  • 打赏
  • 举报
回复
没错,缺少重载函数
Mr_Yang 2005-11-14
  • 打赏
  • 举报
回复
Box2();
box2=box1; 这个好象不能直接等的吧,要重载"=";

33,311

社区成员

发帖
与我相关
我的任务
社区描述
C/C++ 新手乐园
社区管理员
  • 新手乐园社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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