注册 登录
编程论坛 C++教室

C++中的class的一个问题,求指导。

downeychou 发布于 2013-12-07 20:36, 764 次点击
#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

求指导,
 还有个问题,有没有什么程序能使编译时出错的问题用中文表达出来?谢谢!
10 回复
#2
peach54602013-12-07 20:58
你知道什么叫构造函数么?
#3
tianxin_fox2013-12-07 21:45
我们一般都把长宽高之类的定义为私有变量, 然后用构造函数对其赋值
#4
Hassock2013-12-08 15:20
#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;
}
#5
zuojian1682013-12-08 16:23
#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的没有参数的构造函数了
#6
左手拉一只猫2013-12-08 16:56
构造函数学的还不好额,少年。
#7
IT男year2013-12-09 20:01
你在定义对象是调用无参构造函数,所以你要定义一个无参构造函数!
#8
竹风smile2013-12-17 20:27
构造函数有问题!!!
#9
g76131152013-12-17 21:10
#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;
}
#10
g76131152013-12-17 21:20
定义一个类的时间,如果不定义一个构造函数,编译器会自动生成一个没有参数的默认构造函数,而当你设定一个带三个参数的构造函数时,函数则不在生成默认构造函数。
  你的错误为,当你定义类的对象时,所定义的对象时没有参数的对象,而在你的类中只有一个带三个参数的构造函数,因此出错。应改为:
#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函数没有用处,无法从屏幕上输入,可以改为在主函数中输,然后带入参数到对象中。
#11
冷读者2013-12-20 00:17
加上
Box(int h,int w,int l):height(h),width(w),length(l){}
是可以的。
但是定义一个类的对象的时候就要传入值。如Box box1(10,10,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(10,10,10);

 //   box1.get_value();
    cout<<box1.display()<<endl;
    return 0;
}
1