2013-10-09 53 views
0

我有2个类,CLASS的locationdata是CLASS PointTwoD的私有成员。如何访问作为另一个类的私有成员的类的方法

CLASS locationdata

class locationdata 
{ 
    public: 
    locationdata(); //default constructor 
    locationdata(string,int,int,float,float); //constructor 

//setter 
void set_sunType(string); 
void set_noOfEarthLikePlanets(int); 
void set_noOfEarthLikeMoons(int); 
void set_aveParticulateDensity(float); 
void set_avePlasmaDensity(float); 

//getter 
string get_sunType(); 
int get_noOfEarthLikePlanets(); 
int get_noOfEarthLikeMoons(); 
float get_aveParticulateDensity(); 
float get_avePlasmaDensity(); 


static float computeCivIndex(string,int,int,float,float); 
friend class PointTwoD; 

private: 

    string sunType; 
    int noOfEarthLikePlanets; 
    int noOfEarthLikeMoons; 
    float aveParticulateDensity; 
    float avePlasmaDensity; 

}; 

CLASS PointTwoD

class PointTwoD 
{ 
    public: 
    PointTwoD(); 
    PointTwoD(int, int ,locationdata); 

    void set_x(int); 
    int get_x(); 

    void set_y(int); 
    int get_y(); 

    void set_civIndex(float); 
    float get_civIndex(); 

    locationdata get_locationdata(); 



    bool operator<(const PointTwoD& other) const 
{ 
    return civIndex < other.civIndex; 
} 

    friend class MissionPlan; 

private: 
    int x; 
    int y; 
    float civIndex; 
    locationdata l; 

}; 

我主要的方法,我试图访问locationdata的私有成员但是我得到一个错误:的“基础操作 - > '有非指针类型'locationdata'

这就是我如何访问私人会员

int main() 
{ 
    list<PointTwoD>::iterator p1 = test.begin(); 
    p1 = test.begin(); 

    locationdata l = p1 ->get_locationdata(); 
    string sunType = l->get_sunType(); // this line generates an error 

} 

回答

2

这无关私人/公共。您正在使用指针访问运算符->来访问类的成员;你应该用.代替:

string sunType = l.get_sunType(); 
2

这不是访问权限的问题,get_sunType()已经是public了。

l不是指针,你可以通过.运营商访问

更新:

string sunType = l->get_sunType(); // this line generates an error 
//    ^^ 

到:

string sunType = l.get_sunType(); 
//    ^
1

操作->在locationdata没有实现。 您需要使用.操作:

string sunType = l.get_sunType();

勒兹万。

-1

根据您的代码,p1不是参考。

尝试

p1.get_locationdata() 

,而不是

p1->get_locationdata() 
+0

事实上,'p1'是一个迭代器,所以这部分是正确的。问题在于另一个 - 用'l'。 – Angew

+0

我的不好。感谢您指出Agnew。 –

相关问题