K
krzysztof.konopko
I cannot compile the code which defines a std::map type consisting of
built in types and operator<< overload for std::map::value_type.
See the code below - I attach a full example.
Note: if I define map type with my new type (structure) everything is
OK. All compileres I've cheked report an error so I think it is a
problem with my code.
#include <algorithm>
#include <cstddef>
#include <fstream>
#include <iterator>
#include <map>
class A
{
struct S // behavior similar to built-in int
{
int i;
S( int _i ) : i( _i ) {}
};
typedef std::map< int, int > Map1;
typedef std::map< int, S > Map2;
// the function below cannot be found by
std:stream_iterator< Map1::value_type >
friend std:stream &operator<<( std:stream &_ostr, const
Map1::value_type &_value )
{
return _ostr << _value.first << ' ' << _value.second;
}
friend std:stream &operator<<( std:stream &_ostr, const
Map2::value_type &_value )
{
return _ostr << _value.first << ' ' << _value.second.i;
}
Map1 m_map1;
Map2 m_map2;
public:
A(
const int *_keyStart,
const int *_valueStart,
const std::size_t _size
)
{
for ( std::size_t i = 0; i < _size; i++ )
{
m_map1.insert( Map1::value_type( _keyStart, _valueStart ) );
m_map2.insert( Map2::value_type( _keyStart,
S( _valueStart ) ) );
}
}
~A()
{
std:fstream out( "test" );
out << "#### Map1 ####" << std::endl;
// this call does not compile
std::copy(
m_map1.begin(),
m_map1.end(),
std:stream_iterator< Map1::value_type >( out, "\n" )
);
out << "\n#### Map2 ####" << std::endl;
std::copy(
m_map2.begin(),
m_map2.end(),
std:stream_iterator< Map2::value_type >( out, "\n" )
);
}
};
int main()
{
int tab1[] = { 1, 3, 5, 2, 0 };
int tab2[ sizeof tab1 / sizeof *tab1 ] = { 1, 2, 3, 4, 5 };
A a( tab1, tab2, sizeof tab1 / sizeof *tab1 );
}
built in types and operator<< overload for std::map::value_type.
See the code below - I attach a full example.
Note: if I define map type with my new type (structure) everything is
OK. All compileres I've cheked report an error so I think it is a
problem with my code.
#include <algorithm>
#include <cstddef>
#include <fstream>
#include <iterator>
#include <map>
class A
{
struct S // behavior similar to built-in int
{
int i;
S( int _i ) : i( _i ) {}
};
typedef std::map< int, int > Map1;
typedef std::map< int, S > Map2;
// the function below cannot be found by
std:stream_iterator< Map1::value_type >
friend std:stream &operator<<( std:stream &_ostr, const
Map1::value_type &_value )
{
return _ostr << _value.first << ' ' << _value.second;
}
friend std:stream &operator<<( std:stream &_ostr, const
Map2::value_type &_value )
{
return _ostr << _value.first << ' ' << _value.second.i;
}
Map1 m_map1;
Map2 m_map2;
public:
A(
const int *_keyStart,
const int *_valueStart,
const std::size_t _size
)
{
for ( std::size_t i = 0; i < _size; i++ )
{
m_map1.insert( Map1::value_type( _keyStart, _valueStart ) );
m_map2.insert( Map2::value_type( _keyStart,
S( _valueStart ) ) );
}
}
~A()
{
std:fstream out( "test" );
out << "#### Map1 ####" << std::endl;
// this call does not compile
std::copy(
m_map1.begin(),
m_map1.end(),
std:stream_iterator< Map1::value_type >( out, "\n" )
);
out << "\n#### Map2 ####" << std::endl;
std::copy(
m_map2.begin(),
m_map2.end(),
std:stream_iterator< Map2::value_type >( out, "\n" )
);
}
};
int main()
{
int tab1[] = { 1, 3, 5, 2, 0 };
int tab2[ sizeof tab1 / sizeof *tab1 ] = { 1, 2, 3, 4, 5 };
A a( tab1, tab2, sizeof tab1 / sizeof *tab1 );
}