2013-10-05 5 views
1

나는 친구 클래스에서 민간 및 공공 멤버에 액세스 할 수 없습니다

#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 테스트()에서 선언을 변경 한 후, 나는 다음과 같은 오류 얻을 :의 기본 연산 '->'이 아닌 포인터 타입을 가지고, 그것은 무엇을 의미 하는가를하고 그것을 해결하는 방법을

+2

대부분의 vesing 구문 분석이 다시 발생합니다. – goji

+0

새로운 오류가 말하듯이 포인터가 아닌 유형 인 경우,'->'대신'.' 연산자를 사용하십시오. – goji

답변

2

여기이 :

PointTwoD test();

는 함수 선언이 아닌 변수 정의입니다.

당신이 필요합니다

PointTwoD test;

또는에서 C++ 11 :

PointTwoD test{};

대한 추가 정보를 원하시면 http://en.wikipedia.org/wiki/Most_vexing_parse를 참조하십시오.

+0

여전히 작동하지 않습니다. 새 질문이 있습니다. – Computernerd

+0

새 오류는 새로운 질문을 의미합니다. 우리가 이미 고친 오류에 관한이 질문으로. 또한 도움을 요청하기 전에 이전 오류가 수정되었으므로 새로운 오류를 직접 디버깅해야합니다. – goji