"Klasse::Datenelement ist privat" angezeigt, aber ich bewege mich auf es mit eine member-Funktion?

Ich bin ziemlich neu in C++, und ich verstehe nicht, was das auslösen dieses Fehlers:

/home/---/Documents/C++/---_lab2/lab2c.cpp||In function int main()’:|
Line 9: error: float circle::x1 is private
Line 58: error: within this context

Ich weiß, die Daten-member x1 (x2,y1,y2) ist privat, aber ich fahre auf das Objekt myObj mit Funktionen, die Mitglieder der Klasse circle, so sollten Sie nicht immer noch funktionieren? Kann mir jemand erklären, was hier falsch?

#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;
}
Die meisten dieser code ist nicht erforderlich, um das problem anzuzeigen. In diesem besonderen Beispiel, das problem ist leicht zu sehen, aber im Allgemeinen schneiden Sie Ihren code bis hin zu den kleinsten kann man noch zeigt, was das problem ist.

InformationsquelleAutor nmar | 2012-09-30

Schreibe einen Kommentar