构造三角形类
本帖最后由 huhuhu12138 于 2018-1-3 11:39 编辑javascript:;#include<iostream>
using namespace std;
class triangle
{
private:
float x,y,z;
public:
triangle(float a,float b,float c);
void perimeter();
};
int main()
{
triangle s1(float a,float b, float c);
s1.perimeter();
return 0;
}
triangle::triangle(float a,float b,float c)
{
cin>>a>>b>>c;
x=a;y=b;z=c;
}
void triangle::perimeter()
{
float sum;
sum=x+y+z;
cout<<sum<<endl;
}为什么会出现这个error啊???
构造该类对象的时候你要传入实际的值,直接在构造的时候定义三个未初始化的变量无法完成构造
你应该这样
float a = 3, b = 4, c = 5;
triangle s1(a,b,c); triangle(float a,float b,float c);
你的构造函数是带参的;你得住里面传参
修改代码:
#include<iostream>
using namespace std;
class triangle
{
private:
float x,y,z;
public:
triangle();
void perimeter();
};
int main()
{
triangle s1;
s1.perimeter();
return 0;
}
triangle::triangle()
{
cin>>x>>y>>z;
}
void triangle::perimeter()
{
float sum;
sum=x+y+z;
cout<<sum<<endl;
} ba21 发表于 2018-1-3 11:48
triangle(float a,float b,float c);
你的构造函数是带参的;你得住里面传参
嗯嗯嗯,谢谢你
页:
[1]