2014-12-31 2 views
0

에서 친구 함수에서 클래스에 선언 된 보호 된 멤버에 액세스 할 수 없습니다 :다음과 같이 내가 기본 클래스를했습니다 파생 클래스

은 C를 컴파일 // 파생 클래스 - polynomialType.h

#include "arrayListType.h" 

class polynomialType: public arrayListType<double> 
{ 
friend ostream& operator<<(ostream& ,const polynomialType&); 
friend istream& operator>>(istream& ,polynomialType&); 

public: 
polynomialType operator+(const polynomialType&); 
polynomialType operator-(const polynomialType&); 
//polynomialType operator*(const polynomialType&); 
double operator() (double x); 
polynomialType(int size = 100); 
int min(int x,int y) const; 
int max(int x,int y) const; 
}; 

그러나 이후

오데, 나는 오류가있어;

error C2248: 'arrayListType<elemType>::length' : cannot access protected member declared in class 'arrayListType<elemType>' 

나는 해결책을 찾았지만 찾을 수 없습니다. 운영자 >>의 정의가 설명을 위해 제공됩니다.

istream& operator>>(istream is,polynomialType& p) 
{ 
cout << "the degree of this polynomial is" << p.length-1 << endl; 

for (int i = 0; i < p.length; i++) 
{ 
    cout << "enter coefficient of x^" << i <<": "; 
    is >> p.list[i]; 
} 
return is; 
} 

오류는 친구 기능에 대해서만 표시됩니다. 왜 그렇습니까 ??

+0

'public' 정의에'friend' 함수를 넣으려고 했습니까? –

+2

'friend' 선언은 특정 액세스 한정자 아래에있을 필요는 없습니다. – inetknght

답변

1
friend istream& operator>>(istream& ,polynomialType&); 

istream& operator>>(istream is,polynomialType& p) 

절 스트림 함수는 기준 조작 & 잊고 따라서 다른 기능 서명을 갖는다. 그뿐만 아니라 (다시 부작용이있을 수도 있고 없을 수도 있음) 복사와 같은 미묘한 버그가 발생할 수 있습니다.

+1

고마워요 ... 작동했습니다 – bikrathor

+0

스트림은 복사 할 수 없습니다. 여기에서 일어날 수있는 유일한 일은 스트림을 안으로 이동시키는 것입니다 (이는 알아 차리기가 어렵습니다). – Griwes

+0

실제로 STL 스트림은 복사 할 수 없습니다. 비표준 스트림은 카피 가능하다. :: :: std ::'정규화 된 식별자가 없으면 네임 스페이스 문제로 인해 istream이 같은 이름의 사용자 정의 유형으로 해석 될 수 있습니다. – inetknght

관련 문제