私はオペレータを作った場合、 <の友人の のデータ構造体(名前で配列);演算子<<その友人の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(アレイ内のtypedef)を使用します配列構造体のフレンドとして ?
ありがとうございます!しかし、私は何が起こっているのか理解する必要があった。私も編集しました:ostream&operator <<(ostream&os、const _Tarray&var):ostream&operator <<(ostream&os、const配列&var) –
@OsagieOdigieデフォルトではtypenameなしでは、コンパイラはその名前を型名ではないとみなします。 –