나는 친구 클래스에서 민간 및 공공 멤버에 액세스 할 수 없습니다
#include <string>
#include <iostream>
using namespace std;
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();
float computeCivIndex();
friend class PointTwoD; //friend class
private:
string sunType;
int noOfEarthLikePlanets;
int noOfEarthLikeMoons;
float aveParticulateDensity;
float avePlasmaDensity;
};
PointTwoD
라는 친구 클래스가 클래스라는 locationdata이 나는 클래스를 포함한다고 가정한다 PointTwoD라는 다른 클래스가 있습니다 locationdata A와를 개인 회원.#include <iostream>
#include "locationdata.h"
using namespace std;
class PointTwoD
{
public:
PointTwoD();
locationdata location; // class
private:
int x;
int y;
float civIndex;
}};
내 main()에서 PointTwoD 객체를 인스턴스화하고 fromn locationdata의 함수를 사용하려고하면 클래스 유형이 PointTwoD()가 아닌 "test"에서 멤버 'location'에 대한 요청이 발생합니다.).
#include <iostream>
#include "PointTwoD.h"
using namespace std;
int main()
{
int choice;
PointTwoD test();
cout<<test.location->get_sunType; //this causes the error
}
내 질문
내 친구 클래스 작업을 나던 왜1
), 난)이2
를 선언되면 내가 친구로부터 모든 속성이 모든 기능을 사용하여 액세스 할 수 있어야한다고 생각 클래스 PointTwoD에서 클래스 locationdata의 모든 메서드와 속성에 액세스하려면 friend 클래스 대신 상속을 사용합니까 ??먼저 업데이트 : PointTwoD 시험에 PointTwoD 테스트()에서 선언을 변경 한 후, 나는 다음과 같은 오류 얻을 :의 기본 연산 '->'이 아닌 포인터 타입을 가지고, 그것은 무엇을 의미 하는가를하고 그것을 해결하는 방법을
대부분의 vesing 구문 분석이 다시 발생합니다. – goji
새로운 오류가 말하듯이 포인터가 아닌 유형 인 경우,'->'대신'.' 연산자를 사용하십시오. – goji