我对 C++ 很陌生,我不明白是什么触发了这个错误:
/home/---/Documents/C++/---_lab2/lab2c.cpp||In function ‘int main()’:|
Line 9: error: ‘float circle::x1’ is private
Line 58: error: within this context
我知道数据成员 x1(x2,y1,y2 也是)是私有的,但是我正在使用属于类圈子成员的函数对对象 myObj 进行操作,所以它们不应该仍然有效吗?有人可以向我解释这里有什么问题吗?
#include <iostream>
#include <cmath>
#define PI 3.14159
using namespace std;
class circle{
private:
float x1,y1,x2,y2;
protected:
float distance(float x1,float y1,float x2, float y2){
return sqrt(fabs((x2-x1)*(x2-x1))+fabs((y2-y1)*(y2-y1)));
};
public:
float radius(float x1, float y1, float x2, float y2){
float rad = distance(x1,y1,x2,y2);
return rad;
};
float circumference(float rad){
return 2*PI*rad;
};
float area(float rad){
return PI*rad*rad;
};
float populate_classobj(float x1main,float x2main,float y1main,float y2main){
x1 = x1main;
x2 = x2main;
y1 = y1main;
y2 = y2main;
};
};
int main(){
circle myObj;
float x1main,x2main,y1main,y2main;
cout << "Coordinates of center" << endl;
cout << "X: ";
cin >> x1main;
cout << "Y: ";
cin >> y1main;
cout << "Coordinates of point on circle" << endl;
cout << "X: ";
cin >> x2main;
cout << "Y: ";
cin >> y2main;
myObj.populate_classobj(x1main,x2main,y1main,y2main);
cout << "Radius is " << myObj.radius(myObj.x1,myObj.y1,myObj.x2,myObj.y2) << endl;
cout << "Circumference is " << myObj.circumference(myObj.radius(myObj.x1,myObj.y1,myObj.x2,myObj.y2)) << endl;;
cout << "Area is " << myObj.area(myObj.radius(myObj.x1,myObj.y1,myObj.x2,myObj.y2)) << endl;
return 0;
}