Operator overloading for an inner class of a class template causes compilation error
Hi,
could you please answer why the following code causes a compilation error?
How could one avoid the error while keeping the definition of the struct B within the struct A?
Code:
#include <iostream>
template<typename T> struct A {
struct B { T t; };
B b;
};
template<typename T>
std::ostream& operator << (std::ostream& os, const typename A<T>::B& b) {
return os<<"an instance of B";
}
int main() {
A<int> a;
std::cout<<a.b<<std::endl;
return 0;
}
It seems to me that for some reason the compiler can't deduce the type needed. I unsuccessfully tried to compile the code using MS VC++ 2005 compiler and Borland C++ Compiler 5.5.
MS VC++ 2005 compiler says:
binary '<<' : no operator found which takes a right-hand operand of type 'A<T>::B' (or there is no acceptable conversion) with [T=int]
Borland C++ Compiler 5.5 says:
'operator<<' not implemented in type 'std::ostream' for arguments of type 'A<int>::B' in function main()
Thanks!
Alex