C++ 如何访问属于另一个类的私有成员的类的方法
我有两个类,类locationdata是PointTwoD类的私有成员 类位置数据C++ 如何访问属于另一个类的私有成员的类的方法,c++,class,debugging,pointers,linked-list,C++,Class,Debugging,Pointers,Linked List,我有两个类,类locationdata是PointTwoD类的私有成员 类位置数据 class locationdata { public: locationdata(); //default constructor locationdata(string,int,int,float,float); //constructor //setter void set_sunType(string); void set_noOfEarthLikePlanets(int); voi
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
{
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;
};
类PointTwoD
{
公众:
两点();
第二点(int,int,locationdata);
无效集x(int);
int get_x();
无效集y(int);
int get_y();
无效集_civIndex(浮动);
float get_civIndex();
locationdata获取位置数据();
布尔运算符设置位置数据();
string sunType=l->get_sunType();//此行生成错误
}
这不是访问权限问题,get\u sunType()
已经是public
了
l
不是指针,您可以通过
运算符访问它
更新:
string sunType = l->get_sunType(); // this line generates an error
// ^^
致:
这不是访问权限问题,
get\u sunType()
已经是public
了
l
不是指针,您可以通过
运算符访问它
更新:
string sunType = l->get_sunType(); // this line generates an error
// ^^
致:
这与私人/公共部门无关。您正在使用指针访问操作符
->
访问类的成员;您应该改用
:
string sunType = l.get_sunType();
这与私人/公共部门无关。您正在使用指针访问操作符
->
访问类的成员;您应该改用
:
string sunType = l.get_sunType();
操作员
->
在locationdata中没有实现。
您需要使用
运算符:
string sunType=l.get_sunType()代码>
Razvan。操作员->
在locationdata中没有实现。
您需要使用
运算符:
string sunType=l.get_sunType()代码>
Razvan。根据您的代码,p1不是参考
试一试
而不是
p1->get_locationdata()
根据您的代码,p1不是参考
试一试
而不是
p1->get_locationdata()
实际上,p1
是一个迭代器,所以这部分是正确的。另一个问题是l
的问题。实际上,p1
是一个迭代器,因此该部分是正确的。问题出在另一个问题上,即l
。