| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 764 人关注过本帖
标题:C++中的class的一个问题,求指导。
只看楼主 加入收藏
downeychou
Rank: 1
等 级:新手上路
帖 子:7
专家分:0
注 册:2013-11-13
结帖率:100%
收藏
已结贴  问题点数:10 回复次数:10 
C++中的class的一个问题,求指导。
#include <iostream>
using namespace std;
class Box
{public:
void get_value();
int display();
private:
    int height;
    int width;
    int length;
};
void Box::get_value()
{cin>>height;
cin>>width;
cin>>length;}
int  Box::display()
{return (height*width*length);}
int main()
{Box box1;
box1.get_value();
cout<<box1.display()<<endl;
return 0;
}


这是一个用键盘输入长宽高,求体积的题,为什么我在public中定义一个构造函数:
Box(int h,int w,int l):height(h),width(w),length(l){}
就不能运行了呢?
错误显示为 error C2512: 'Box' : no appropriate default constructor available

求指导,
 还有个问题,有没有什么程序能使编译时出错的问题用中文表达出来?谢谢!
搜索更多相关主题的帖子: private display include public return 
2013-12-07 20:36
peach5460
Rank: 15Rank: 15Rank: 15Rank: 15Rank: 15
来 自:武汉
等 级:贵宾
威 望:30
帖 子:2780
专家分:6060
注 册:2008-1-28
收藏
得分:0 
你知道什么叫构造函数么?

我总觉得授人以鱼不如授人以渔...
可是总有些SB叫嚣着:要么给代码给答案,要么滚蛋...
虽然我知道不要跟SB一般见识,但是我真的没修炼到宠辱不惊...
2013-12-07 20:58
tianxin_fox
Rank: 1
等 级:新手上路
帖 子:8
专家分:7
注 册:2013-12-7
收藏
得分:0 
我们一般都把长宽高之类的定义为私有变量, 然后用构造函数对其赋值
2013-12-07 21:45
Hassock
Rank: 1
等 级:新手上路
帖 子:1
专家分:0
注 册:2013-12-8
收藏
得分:0 
#include <iostream>
using namespace std;
class Box
{
public:
void get_value();
int display();
private:
    int height;
    int width;
    int length;
};
void Box::get_value()
{
cin>>height;
cin>>width;
cin>>length;
}
int  Box::display(){return (height*width*length);}
int main()
{
Box box1;
box1.get_value();
cout<<box1.display()<<endl;
return 0;
}
2013-12-08 15:20
zuojian168
Rank: 2
等 级:论坛游民
帖 子:16
专家分:27
注 册:2013-3-20
收藏
得分:10 
#include <iostream>
using namespace std;
class Box
{public:
Box(int h,int w,int l):height(h),width(w),length(l){}
void get_value();
int display();
private:
    int height;
    int width;
    int length;
};
void Box::get_value()
{/*cin>>height;
cin>>width;
cin>>length;*/}
int  Box::display()
{return (height*width*length);}
int main()
{Box box1(1,1,1);
//box1.get_value();
cout<<box1.display()<<endl;
return 0;
}
因为你写的是有参数的构造函数,声明对象Box box1(1,1,1)必须要加参数,或者你也可以写没有参数的构造函数,这样就可以调用Box box1的没有参数的构造函数了
2013-12-08 16:23
左手拉一只猫
Rank: 4
来 自:杭州
等 级:业余侠客
帖 子:70
专家分:250
注 册:2013-10-27
收藏
得分:0 
构造函数学的还不好额,少年。

我能帮你的只能到这里了。。。
2013-12-08 16:56
IT男year
Rank: 3Rank: 3
等 级:论坛游侠
威 望:1
帖 子:82
专家分:106
注 册:2013-6-9
收藏
得分:0 
你在定义对象是调用无参构造函数,所以你要定义一个无参构造函数!
2013-12-09 20:01
竹风smile
Rank: 1
等 级:新手上路
帖 子:2
专家分:1
注 册:2013-12-15
收藏
得分:0 
构造函数有问题!!!
2013-12-17 20:27
g7613115
Rank: 1
等 级:新手上路
帖 子:2
专家分:0
注 册:2013-12-17
收藏
得分:0 
#include<iostream>
using namespace std;
class box
{
 public:
    void set(int h,int w,int l){height=h;width=w;length=l;}
    int get(){return height*width*length;}
  private:
    int height;
    int width;
    int length;
};
int main()
{
  box a;
  int length,width,heigth;
  cout<<"请输入您所求箱子的高:";
  cin>>heigth;
 cout<<"请输入您所求箱子的宽:";
  cin>>width;
 cout<<"请输入您所求箱子的长:";
  cin>>length;
 a.set(heigth,width,length);
cout<<"所求体积为:"<<a.get()<<endl;
  return 0;
}
2013-12-17 21:10
g7613115
Rank: 1
等 级:新手上路
帖 子:2
专家分:0
注 册:2013-12-17
收藏
得分:0 
定义一个类的时间,如果不定义一个构造函数,编译器会自动生成一个没有参数的默认构造函数,而当你设定一个带三个参数的构造函数时,函数则不在生成默认构造函数。
  你的错误为,当你定义类的对象时,所定义的对象时没有参数的对象,而在你的类中只有一个带三个参数的构造函数,因此出错。应改为:
#include <iostream>
using namespace std;
class Box
{public:
Box(int h,int w,int l):height(h),width(w),length(l){};
void get_value();
int display();
private:
    int height;
    int width;
    int length;
};
void Box::get_value()
{cin>>height;
cin>>width;
cin>>length;}
int  Box::display()
{return (height*width*length);}
int main()
{Box box1(1,2,3);
box1.get_value();
cout<<box1.display()<<endl;
return 0;
}
不过这样的话,参数为事先设定好的,你的get value函数没有用处,无法从屏幕上输入,可以改为在主函数中输,然后带入参数到对象中。
2013-12-17 21:20
快速回复:C++中的class的一个问题,求指导。
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.078298 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved