?
=?ISO-8859-1?Q?Alexander_B=FCrger?=
Hi,
I have a little problem with classes in a namespace and an operator
defined for these classes outside that namespace. Example:
// -------------------------------------------------------------------
#include <iostream>
#include <vector>
namespace N {
struct A { int a; };
A& operator<<(A& a, int i)
{ std::cout << "i=" << i << '\n'; return a; }
A& operator<<(A& a, float f)
{ std::cout << "f=" << f << '\n'; return a; }
}
template<typename T>
void WriteVector(N::A& a, std::vector<T> const& v)
{
typename std::vector<T>::const_iterator it;
for( it=v.begin(); it!=v.end(); ++it )
a << *it;
}
template<typename T>
inline N::A& operator<<(N::A& a, std::vector<T> const& v)
{ WriteVector( a, v ); return a; }
namespace N {
void go() {
std::vector<int> v;
for( int i=0; i<5; ++i )
v.push_back( 1<<i );
A a;
WriteVector( a, v );
a << v;
}
}
int main()
{
N::go();
}
// -------------------------------------------------------------------
The compiler (g++) gives me the following error message:
######################################################################
In function 'void N::go()':
error: no match for 'operator<<' in 'a << v'
note: candidates are: N::A& N:perator<<(N::A&, int)
note: N::A& N:perator<<(N::A&, float)
######################################################################
So while the operator<< template is not found, the WriteVector version
is. How can I help the compiler to find the operator<< version?
Thanks for your advice,
Alexander
PS: For email reply, please remove the six appropriate letters from the
given address.
I have a little problem with classes in a namespace and an operator
defined for these classes outside that namespace. Example:
// -------------------------------------------------------------------
#include <iostream>
#include <vector>
namespace N {
struct A { int a; };
A& operator<<(A& a, int i)
{ std::cout << "i=" << i << '\n'; return a; }
A& operator<<(A& a, float f)
{ std::cout << "f=" << f << '\n'; return a; }
}
template<typename T>
void WriteVector(N::A& a, std::vector<T> const& v)
{
typename std::vector<T>::const_iterator it;
for( it=v.begin(); it!=v.end(); ++it )
a << *it;
}
template<typename T>
inline N::A& operator<<(N::A& a, std::vector<T> const& v)
{ WriteVector( a, v ); return a; }
namespace N {
void go() {
std::vector<int> v;
for( int i=0; i<5; ++i )
v.push_back( 1<<i );
A a;
WriteVector( a, v );
a << v;
}
}
int main()
{
N::go();
}
// -------------------------------------------------------------------
The compiler (g++) gives me the following error message:
######################################################################
In function 'void N::go()':
error: no match for 'operator<<' in 'a << v'
note: candidates are: N::A& N:perator<<(N::A&, int)
note: N::A& N:perator<<(N::A&, float)
######################################################################
So while the operator<< template is not found, the WriteVector version
is. How can I help the compiler to find the operator<< version?
Thanks for your advice,
Alexander
PS: For email reply, please remove the six appropriate letters from the
given address.