I have a problem with inner classes in class templates. I have a template class (say: Matrix<T>
), and a subclass (say: Matrix<T>::Row
). Now I want to to write a function which operates on instances of the subclass (say: negate(Matrix<T>::Row &)
). I tried to declare the function with template<class T> negate(typename Matrix<T>::Row &)
, but when I try to use it, the compiler tells me that it cannot find a match.
Here's an abstract example:
template<class T>
class A
{
public:
class B
{
};
};
template<class T>
void x(typename A<T>::B &)
{
}
int main()
{
A<int>::B b;
x(b); // doesn't work: Error: Could not find a match
// for x<T>(A<int>::B) needed in main().
x<int>(b); // works fine
}
Why does the compiler does not manage to find x
in the first case? Is there a way to modify this that it works (without explicitly specifying the type int
)?
(I also have similar problems where x
is of the form template<class T, class S> void x(typename A<T>::B &, const S &);
, whence I would really like not to be forced to explicitly name all types while doing the call.)
I have tried this with g++ 4.4.3, g++ 4.5.2, and Sun Studio 5.9, all give the same result. Thanks a lot in advance for anything helpful!
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…