0

私は 2 つのクラスを持っています。CLASS locationdata は CLASS PointTwoD のプライベート メンバーです。

CLASSロケーションデータ

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

}
4

4 に答える 4

2

これはアクセス権の問題ではなく、get_sunType()すでにpublicあります。

l.はポインタではありません。演算子でアクセスできます

アップデート:

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

に:

 string sunType = l.get_sunType(); 
 //                ^
于 2013-10-09T08:28:04.493 に答える
2

これは私立/公立とは関係ありません。ポインター アクセス演算子->を使用して、クラスのメンバーにアクセスしています。.代わりに使用する必要があります:

string sunType = l.get_sunType();
于 2013-10-09T08:28:35.457 に答える
1

オペレーター->は、ロケーションデータに実装されていません。.演算子を使用する必要があります:

string sunType = l.get_sunType();

ラズヴァン。

于 2013-10-09T08:29:07.410 に答える
-1

コードによると、 p1 は参照ではありません。

試す

p1.get_locationdata()

それ以外の

p1->get_locationdata()
于 2013-10-09T08:30:54.213 に答える