Re: Howto declare a friend function to a nested class
jdurancomas@gmail.com wrote:
[...]
Improved source code:
#include <iostream>
template <typename T>
class A
{
public:
class B;
};
/* Example of template function */
template <typename T>
T add(T a, T b) {return a+b;}
template <typename T>
bool operator == (const typename A<T>::B &b1,
const typename A<T>::B &b2);
template <typename T>
class A<T>::B
{
public:
B(int a);
friend bool operator == <> (const typename A<T>::B &b1,
Should be
friend bool operator == <T> ...
const typename A<T>::B &b2); // Line 29
private:
int aa;
};
template <typename T>
A<T>::B::B(int a):
aa(a)
{}
template <typename T>
bool operator == (const typename A<T>::B& b1,
const typename A<T>::B& b2)
{
return b1.aa == b2.aa;
}
class C {};
int main()
{
A<C>::B b1(4), b2(5);
bool res = (b1 == b2); // Line 57
There is no way for the compiler to determine that the template
operator should be used because from the expression 'b1 == b2'
the compiler cannot deduce the 'C' for the template -- the context
is not one of the deducible contexts.
return 0;
}
The message error from compiler is:
nested.cpp: In instantiation of 'A<C>::B':
nested.cpp:55: instantiated from here
nested.cpp:29: error: template-id 'operator==<>' for 'bool
operator==(const A<C>::B&, const A<C>::B&)' does not match any
template declaration
That can be rectified by placing 'T' in the angle brackets, see
above.
nested.cpp: In function 'int main()':
nested.cpp:57: error: no match for 'operator==' in 'b1 == b2'
This cannot be corrected because the compiler canno deduce the 'T'
for the template operator==
I've googled a litle abut how declare template functions as friends,
and It looks likes that the current syntax is right.
Apparently it wasn't.
V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask