Archived

This topic is now archived and is closed to further replies.

Burning_Ice

template friend operators

Recommended Posts

well i recently switched to Visual Studio .Net 2003 from VS5, and since it's a lot more standard-compliant its a bit differently behaving in regards to templates, and now i just dont get friend operators of templated classes to run anymore. i have this code:
template < class T > class Pointer_to_const
{
	friend bool operator==( Pointer_to_const<T> const &, Pointer_to_const<T> const & );
...
};
...
template < class T > bool operator== ( Pointer_to_const<T> const &op1, Pointer_to_const<T> const &op2 )
{
      // ... access some protected members of Pointer_to_const

}
but this gives me error LNK2019: unresolved external symbol "bool __cdecl operator==(class Pointer_to_const< int > const &,class Pointer_to_const< int > const &)" (when testing a pointer to int) if i make it a normal function i get the same error, but when i change the friend declaration to friend bool f< T > ( Pointer_ ... ); it works. but how do i do this with operators?? a friend bool operator== < T > ( ... ) just produces some more errors. so how do you declare those operators as friends? or isn't this possible anymore with the current standard? ( i had hoped to find the solution to my problem by looking at how the STL does it, but it uses either member-operators or the operators call public compare functions, no friend operators to find) any help would be appreciated of course Edit: fixed the < > tags

Share this post


Link to post
Share on other sites
Inline the friend function definition in the class body.


template < class T > class Pointer_to_const
{
friend bool operator==( Pointer_to_const<T> const &,
Pointer_to_const<T> const & )
{
return true;
}
};



[ Start Here ! | How To Ask Smart Questions | Recommended C++ Books | C++ FAQ Lite | Function Ptrs | CppTips Archive ]
[ Header Files | File Format Docs | LNK2001 | C++ STL Doc | STLPort | Free C++ IDE | Boost C++ Lib | MSVC6 Lib Fixes ]



[edited by - Fruny on July 7, 2003 9:52:34 AM]

Share this post


Link to post
Share on other sites