2012-12-04 3 views
0
//**** Build of configuration Debug for project Calculator **** 

    **** Internal Builder is used for build    **** 
g++ -O0 -g3 -Wall -c -fmessage-length=0 -o src\Calculator.o ..\src\Calculator.cpp 
..\src\/Calculator.h: In function 'std::ostream& operator<<(std::ostream&, CComplex)': 
    ..\src\/Calculator.h:38:9: error: 'float CComplex::m_imaginary' is private 
    ..\src\Calculator.cpp:79:8: error: within this context 
    ..\src\/Calculator.h:37:9: error: 'float CComplex::m_real' is private 
    ..\src\Calculator.cpp:81:12: error: within this context 
     ..\src\/Calculator.h:38:9: error: 'float CComplex::m_imaginary' is private 
     ..\src\Calculator.cpp:81:31: error: within this context 
    ..\src\/Calculator.h:37:9: error: 'float CComplex::m_real' is private 
     ..\src\Calculator.cpp:85:12: error: within this context 
     ..\src\/Calculator.h:38:9: error: 'float CComplex::m_imaginary' is private 
     ..\src\Calculator.cpp:85:31: error: within this context 
Build error occurred, build is stopped 
Time consumed: 687 ms. 

아무도 도와 줄 수 있습니까? 액세스를 허용하지 않는 개인 기능에 액세스하려고합니다.개인 기능에 액세스 할 수 없습니다.

+0

왜 개인용 기능에 액세스하려고합니까? 위는 전적으로 예상되는 것 같습니다. –

답변

2

글쎄, 만약 그렇지 않다면, 이것은 훌륭한 질문이었을 것이다.



클래스 멤버의 목록을 이전, 민간 키워드는 그 회원은 멤버 함수와 클래스의 친구에서 액세스 할 수 있는지 지정합니다. 이것은 다음 액세스 지정자 또는 클래스의 끝인 까지 선언 된 모든 멤버에 적용됩니다.

멤버 함수는 클래스 외부에서 액세스하려고하기 때문에 액세스 할 수 없습니다. 위에서 설명한 것처럼 private 키워드는이를 방지하기 위해 사용됩니다.

당신이 다음 공용 키워드를 사용하여 그것을 공개 방법을 내리는 데 필요한 클래스 외부에서 액세스를 필요로 할 경우.

는 private 키워드에 대한 some examples and explanation 여기를보세요.


당신을 찾고 오류가 나는 문제가 운영자 < < 당신의 오버로드에있다 생각합니다. 운영자는 그 자체로 문제를 해결하는 친구 기능으로 만 오버로드 될 수 있습니다.

friend std::ostream& operator<<(std::ostream&, CComplex);

+0

당신이 나를 웃게 만들었 기 때문에 일어났습니다. 사실입니다. 그러나 아직 완전한 대답은 아닙니다. @harsha에 개인/보호/공개 기능의 차이점을 대략 설명 할 수도 있습니다. 그렇다면 계속 유지할 수 있습니다;) – giorgio

+1

@giorgio 질문을 upvote 유혹조차했지만, 나는 스스로 멈춰야했다 :) – asheeshr

1

당신은 아마 CComplex 클래스의 operator<< 친구를 만들고 싶어. 다음과 같은 내용 :

class CComplex { 
    ... 
    // It doesn't matter whether this declaration is on a private, 
    // public or protected section of the class. The friend function 
    // will always have access to the private data of the class. 
    friend std::ostream& operator<<(std::ostream&, CComplex); 
};