C++类内的构造函数无法访问私有成员,怎么回事??

#include<iostream>#include<string>#include<cmath>usingnamespacestd;classShape{public:... #include <iostream>
#include <string>
#include <cmath>
using namespace std;

class Shape
{
public:virtual float area() const=0;
virtual void shapename() const=0;
};

class Circle:public Shape
{
public:
Circle()
{
radius=0;
}
Circle(float r):radius(r){};
virtual void shapename() const
{
cout<<"Circle"<<endl;
}
virtual float area() const;
private:
float radius;
};
float Circle::area() const
{
return 3.14159*radius*radius;
};

class Rectangle:public Shape
{
public:
Rectangle()
{
width=0;
longth=0;
}
Rectangle(float a,float b)
{
width=a;
longth=b;
}
virtual void shapename() const
{
cout<<"Rectangle"<<endl;
}
virtual float area() const;
private:
float width;
float longth;
};
float Rectangle::area() const
{
return width*longth;
}

class Triangle:public Shape
{
Triangle()
{
s1=0;
s2=0;
s3=0;
};
Triangle(float a,float b,float c)
{
s1=a;
s2=b;
s3=c;
};
virtual void shapename() const
{
cout<<"Triangle"<<endl;
}
virtual float area() const;
private:
float s1;
float s2;
float s3;
};
float Triangle::area() const
{
return sqrt(((s1+s2+s3)/2)*((s1+s2+s3)/2-s1)*((s1+s2+s3)/2-s2)*((s1+s2+s3)/2-s3));
}

int main()
{
Circle c1(3.6);
Rectangle r1(1.5,9.8);
Triangle t1(2.5,3.5,4.1);

Shape *p;
p=&c1;
p->shapename();
cout<<p->area()<<endl;

p=&r1;
p->shapename();
cout<<p->area()<<endl;

p=&t1;
p->shapename();
cout<<p->area()<<endl;

cin.get();

return 0;

};

错误:Triangle t1(2.5,3.5,4.1);
编译环境 vs2008
1>error C2248: “Triangle::Triangle”: 无法访问 private 成员(在“Triangle”类中声明)
展开
 我来答
painfulmiss
2013-07-04 · TA获得超过247个赞
知道小有建树答主
回答量:244
采纳率:0%
帮助的人:196万
展开全部
你Triangle的构造函数不是public的啊
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式