错误:请求非类别成员
问题描述:
我只是在尝试一个计算立方体体积的简单程序。我已经在main中声明了这个对象,当我尝试访问带有用户输入参数的类的函数时,它显示一个错误:请求'vol'中的成员'volume cube',它是非类类型'Vellimi()'。为什么会发生?错误:请求非类别成员
#include <iostream>
using namespace std;
class Vellimi {
private:
double width;
double height;
double length;
public:
Vellimi(double,double,double);
double volume_cube (double width,double height,double length)
{
return width*height*length;
}
};
Vellimi::Vellimi(double a,double b,double c){
width=a;
height=b;
length=c;
}
int main()
{
double x,y,z;
Vellimi vol();
cout<<"Input the width : "<<endl;
cin>>x;
cout<<"Input the height : "<<endl;
cin>>y;
cout<<"Input the length : "<<endl;
cin>>z;
cout<<"The volume is "<<vol.volume_cube(x,y,z)<<endl;
return 0;
}
答
您刚刚成为C++的Most Vexing Parse
更改此的受害者:
Vellimi vol();
到
Vellimi vol(0, 0, 0); //or
//Vellimi vol; Unfortunately, you have no default constructor
_'Vellimi体积();'_其实声明的函数。只要写“Vellimi vol;'。 –
@πάνταῥεῖ我试过了,它仍然显示错误 –
对不起,我应该写'Vellimi vol(0.0,0.0,0.0);'因为'Vellimi'没有默认的构造函数。你仍然有一个函数声明,因此会产生令人困惑的错误信息。 –