C++错误,求大虾帮忙找找。编译无错误,连接有两个错,奇怪了!

来源:百度知道 编辑:UC知道 时间:2024/06/14 12:40:28
/*习题6:求三个长方柱的体积,基于对象的程序,用成员函数实现*/
#include<iostream>
using namespace std;
class Box
{
public:
Box(float l=0,float w=0,float h=0);
void get_data(void);
void display(void);
private:
float volume(void);
float length;
float width;
float height;
float temp;
}box1,box2,box3;

int main()
{
box1.get_data();
box2.get_data();
box3.get_data();
box1.display();
box2.display();
box3.display();
return 0;
}

void Box::get_data(void)
{
cout<<"Please input the Box's length,width and height in order:"<<endl;
cin>>length>>width>>height;
}

void Box::display(void)
{
temp=volume();
cout<<"The box's volume is: "<<temp<<endl;
}

float Box::volume(void)
{
return(length*width*height);
}
编译没有错误:<

BOX类的构造函数书写错误,去掉以下语句:

Box(float l=0,float w=0,float h=0);

其实只有一个错误,第二关错误就是应为你第一个错误而造成1120.
在就是,你没有定义你在类声明里面生命的Box::Box,你应该也把这个函数写出来。
C++可以帮你生成并且实现一个缺省的构造函数,但前提是你没有别的构造函数存在。现在你有啦一个包含三个参数的构造函数生命,缺省构造函数就不会有。你就必须把你的生命实现。

#include<iostream>
using namespace std;
class Box
{
public:
Box(float l=0,float w=0,float h=0):length(l),width(w),height(h){};
void get_data(void);
void display(void);
private:
float volume(void);
float length;
float width;
float height;
float temp;
}box1,box2,box3;

int main()
{
box1.get_data();
box2.get_data();
box3.get_data();
box1.display();
box2.display();
box3.display();
return 0;
}

void Box::get_data(void)
{
cout<<"Please input the Box's length,width and height in order:"<<endl;
cin>>length>>width>>height