Re: problems with STL iterator decl (map of boost::function)
On 30/11/2010 14:56, Fuzzy Coder wrote:
Hello there.
I am getting an error when trying to compile a program (see below) .
The compiler output is :
lwb.cc: In function `void op(std::map<int, boost::function2<bool, const
TT&, const TT&>, std::less<int>, std::allocator<std::pair<const int,
boost::function2<bool, const TT&, const TT&> > > >, TT, TT)':
lwb.cc:16: error: expected `;' before "it"
lwb.cc: In function `void op(std::map<int, boost::function2<bool, const
TT&, const TT&>, std::less<int>, std::allocator<std::pair<const int,
boost::function2<bool, const TT&, const TT&> > > >, TT, TT) [with TT =
int]':
lwb.cc:30: instantiated from here
lwb.cc:16: error: dependent-name ` std::map<int,boost::function2<bool,
const TT&, const TT&>,std::less<int>,std::allocator<std::pair<const int,
boost::function2<bool, const TT&, const TT&> > > >::iterator' is parsed
as a non-type, but instantiation yields a type
lwb.cc:16: note: say `typename std::map<int,boost::function2<bool, const
TT&, const TT&>,std::less<int>,std::allocator<std::pair<const int,
boost::function2<bool, const TT&, const TT&> > > >::iterator' if a type
is meant
The usual compiler messages when STL components are being used. :-(
The declaration of the iterator "it" appears to be ok.
I was wondering whether I could construct a template typedef X
for the map :
typedef ... template<class NTD> ...
map<int, boost::function2<bool, NTD const&, NTD const& > >
NM ;
And then change "op" to :
template<class TT>
void
op(NM<TT> Map,
TT V1, TT V2)
{
NM<TT>::iterator it ;
typename NM<TT>::iterator it;
}
in an attempt to remove any confusion that the compiler may be
having with the original code. But I am unsure how to make such a
typedef (if even possible) .
Any info/ptrs on how to remove the error, would be much appreciated.
Regards
-------------------------------------------------------
#include <boost/function.hpp>
#include <map>
#include <boost/lambda/lambda.hpp>
#include <iostream>
using namespace std ;
using namespace boost::lambda ;
template<class TT>
void
op(map<int, boost::function2<bool, TT const&, TT const& > > Map,
TT V1, TT V2)
{
map<int, boost::function2<bool, TT const&, TT const& > >::iterator it ;
Again, you need typename here. Read up on dependent types, as Victor
suggests.
Cheers,
Stu
}
main()
{
typedef boost::function2<bool, int const&, int const& > TD ;
map<int,TD> m ;
m[1] = ( _1 < _2 ) ;
m[2] = ( _1 > _2 ) ;
m[3] = ( _1 == _2 ) ;
op(m,2,2) ;
}