나는 그 연산자를 만들면 < <의 친구 의 데이터 구조체 (이름으로 배열);연산자 << 친구의 IT 구성원을 사용할 수 없다. 배열
//Forward Declarations
template<typename S, typename T>
struct array;
template<typename U, typename V>
ostream& operator<< (ostream& ous, const array<U, V>& t);
그러면 다음과 같은 작업을 수행 할 수 있습니다. < <
//operator<< is a friend of struct array{} already
template<typename T, typename U>
ostream& operator<< (ostream& os, const array<T, U>& var){
if(var){
/*Error: 'IT' was not declared in this scope*/
for(IT it = var.data.begin(); it != var.data.end(); it++){
/*and i thought i need not redeclare IT before using it
since operator<< is a friend of array already*/
}
}
else{cout << "empty";}
return os;
}
이제 오퍼레이터의 구현 내부 여기 어레이의 구현 :이처럼 - 운전 테스트 할 때
/*explicit (full) specialization of array for <type, char>*/
template<>
template<typename Key>
struct array<Key, char>{
//data members
map<const Key, string> data;
typedef map<const Key, string>::iterator IT;
//member function
friend ostream& operator<< <>(ostream& ous, const array& t);
//other stuff/functions
};
마지막으로, 컴파일러 화가;
void Test(){
array<int, char> out;
out[1] = "one"; //Never mind. this has been taken care of
out[2] = "two";
cout << out; //Error: 'IT' was not declared in this scope
}
질문 : 정확히 내가 연산자를 선언 한 후에도, 왜 내가 dirrectly 액세스가 에게 IT (배열 내 타입 정의)를 사용할 수 있습니다, 잘못하고, 또는 누구인지 < < (IT를 요청하는) 어레이 구조체의 친구로?
감사합니다. 그러나 나는 무슨 일이 일어나는지 이해할 필요가있었습니다. 또한 ostream & operator << (ostream & os, const _Tarray & var) : ostream & operator << (ostream & os, const 배열 & var) 연산자를 수정했습니다. –
@OsagieOdigie 기본적으로 typename없이 컴파일러는 이름을 형식 이름이 아닌 것으로 간주합니다. –