On Feb 15, 6:13 pm, saneman
> But if I try to overload the + operator I get the same error:
> template< class A > class BOB;
> template< class A >
> BOB operator+(typename BOB::difference_type, BOB const& );
> template
> class BOB {
> private:
> typedef BOB iterator;
> public:
> typedef typename A::difference_type difference_type;
> friend iterator operator + <> (difference_type i, const
> iterator& it);
>
> // Added overload of + operator
> iterator operator + (const difference_type i) const {
> return iterator(1);
> }
>
> };
> When Compiling I get:
> error: declaration of 'operator+' as non-function
> error: expected ';' before '<' token
> Where the error refers to:
> friend iterator operator + <> (difference_type i, const
> iterator& it);
Just a guess, but the operator+ member is hiding the global
operator+ template declaration, so once again, the compiler
doesn't know that it is dealing with a template.
> Does that mean that overloading of template operators are impossible?
Not at all, but name lookup for operators is a bit special. I
find it good practice to define most operators as just calling
named functions, putting all of the binary operators in global
scope. (The one exception would be the assignment operators.)
=46rom what you've posted, I gather that BOB is in fact an
iterator (into itself?). A random access iterator, since it
supports addition of a difference_type. What's wrong with
defining a member operator+=3D, and then, after the class template
definition:
template< typename A >
BOB
operator+( BOB const& lhs,
typename BOB::difference_type rhs )
{
BOB result( lhs ) ;
result +=3D rhs ;
return result ;
}
template< typename A >
BOB
operator+( typename BOB::difference_type lhs,
BOB const& rhs,
{
BOB result( rhs ) ;
result +=3D lhs ;
return result ;
}
Or having defined operator+=3D, add the following two definitions
directly in the class:
friend iterator
operator+( iterator const& lhs, difference_type rhs )
{
BOB result( lhs ) ;
result +=3D rhs ;
return result ;
}
friend iterator
operator+( difference_type rhs, iterator const& lhs )
{
BOB result( rhs ) ;
result +=3D lhs ;
return result ;
}
(In the above, the keyword friend is used to allow defining the
functions in the class without making them members, and not to
allow access to private members. The Barton and Nackman trick.)
FWIW: I usually provide such operators by inheriting from a
class template which defines them, as above. So all I'd have to
write is:
template< typename A >
class BOB
: public Gabi::MixedArithmeticOperators< BOB< A >,
typename A::difference_type >
{
// ...
// but with a public +=3D( difference_type )
} ;
The case comes up so often, it's worth having a generic
solution.
--
James Kanze (GABI Software) email:james.kanze@gmail.com
Conseils en informatique orient=E9e objet/
Beratung in objektorientierter Datenverarbeitung
9 place S=E9mard, 78210 St.-Cyr-l'=C9cole, France, +33 (0)1 30 23 00 34