[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.c++

Invoking a member function over the elements in a map

zdravko.monov

10/8/2008 11:04:00 AM

I have a map and want to invoke an object member function over each
element:

typedef map<string, pair<int, int> > table_t;
table_t table;

class delay_analyzer {
void calculate_conn(pair<string, pair<int, int> >&);

void delay_analyzer::process()
{
for_each
(
table.begin(), table.end(),
bind1st
(
mem_fun_ref(&delay_analyzer::calculate_conn, this)
)
);
}
};

But my compiler says:
msr.cpp: In member function ‘void delay_analyzer::process()’:
msr.cpp:108: error: no matching function for call to ‘mem_fun_ref(void
(delay_analyzer::*)(std::pair<std::basic_string<char,
std::char_traits<char>, std::allocator<char> >, std::pair<int, int>
>&), delay_analyzer* const)’

Any ideas what to do?
5 Answers

Leandro Melo

10/8/2008 11:55:00 AM

0

On 8 out, 08:04, khalderon <zdravko.mo...@gmail.com> wrote:
> I have a map and want to invoke an object member function over each
> element:
>
> typedef map<string, pair<int, int> > table_t;
> table_t table;
>
> class delay_analyzer {
>         void calculate_conn(pair<string, pair<int, int> >&);
>
> void delay_analyzer::process()
> {
>         for_each
>         (
>                 table.begin(), table.end(),
>                 bind1st
>                 (
>                         mem_fun_ref(&delay_analyzer::calculate_conn, this)
>                 )
>         );
>
> }
> };
>
> But my compiler says:
> msr.cpp: In member function ‘void delay_analyzer::process()’:
> msr.cpp:108: error: no matching function for call to ‘mem_fun_ref(void
> (delay_analyzer::*)(std::pair<std::basic_string<char,
> std::char_traits<char>, std::allocator<char> >, std::pair<int, int>
>
> >&), delay_analyzer* const)’
>
> Any ideas what to do?

Hi.

Initially, shouldn't bind1st take two arguments? Then, shouldn't
mem_fun_ref take only one argument? So, you might have mixed things
around...

--
Leandro T. C. Melo

zdravko.monov

10/8/2008 12:43:00 PM

0

oops. I must have made a typo. When I return home, I will give it a
try...

zdravko.monov

10/8/2008 3:56:00 PM

0

On Oct 8, 2:55 pm, Leandro Melo <ltcm...@gmail.com> wrote:
> On 8 out, 08:04, khalderon <zdravko.mo...@gmail.com> wrote:
>
>
>
> > I have a map and want to invoke an object member function over each
> > element:
>
> > typedef map<string, pair<int, int> > table_t;
> > table_t table;
>
> > class delay_analyzer {
> >         void calculate_conn(pair<string, pair<int, int> >&);
>
> > void delay_analyzer::process()
> > {
> >         for_each
> >         (
> >                 table.begin(), table.end(),
> >                 bind1st
> >                 (
> >                         mem_fun_ref(&delay_analyzer::calculate_conn, this)
> >                 )
> >         );
>
> > }
> > };
>
> > But my compiler says:
> > msr.cpp: In member function ‘void delay_analyzer::process()’:
> > msr.cpp:108: error: no matching function for call to ‘mem_fun_ref(void
> > (delay_analyzer::*)(std::pair<std::basic_string<char,
> > std::char_traits<char>, std::allocator<char> >, std::pair<int, int>
>
> > >&), delay_analyzer* const)’
>
> > Any ideas what to do?
>
> Hi.
>
> Initially, shouldn't bind1st take two arguments? Then, shouldn't
> mem_fun_ref take only one argument? So, you might have mixed things
> around...
>
> --
> Leandro T. C. Melo

I am afraid this was not the only problem. I get these errors:
/usr/include/c++/4.2/bits/stl_function.h: At global scope:
/usr/include/c++/4.2/bits/stl_function.h: In instantiation of
‘std::binder1st<std::mem_fun1_ref_t<void, delay_analyzer,
std::pair<std::basic_string<char, std::char_traits<char>,
std::allocator<char> >, std::pair<int, int> >&> >’:
msr.cpp:106: instantiated from here
/usr/include/c++/4.2/bits/stl_function.h:405: error: forming reference
to reference type ‘std::pair<std::basic_string<char,
std::char_traits<char>, std::allocator<char> >, std::pair<int, int>
>&’
/usr/include/c++/4.2/bits/stl_function.h:411: error: forming reference
to reference type ‘std::pair<std::basic_string<char,
std::char_traits<char>, std::allocator<char> >, std::pair<int, int>
>&’
/usr/include/c++/4.2/bits/stl_function.h: In function
‘std::binder1st<_Operation> std::bind1st(const _Operation&, const
_Tp&) [with _Operation = std::mem_fun1_ref_t<void, delay_analyzer,
std::pair<std::basic_string<char, std::char_traits<char>,
std::allocator<char> >, std::pair<int, int> >&>, _Tp =
delay_analyzer*]’:
msr.cpp:106: instantiated from here
/usr/include/c++/4.2/bits/stl_function.h:421: error: no matching
function for call to ‘delay_analyzer::delay_analyzer(delay_analyzer*
const&)’
msr.cpp:92: note: candidates are: delay_analyzer::delay_analyzer(const
char*)
msr.cpp:11: note: delay_analyzer::delay_analyzer(const
delay_analyzer&)
/usr/include/c++/4.2/bits/stl_algo.h: In function ‘_Function
std::for_each(_InputIterator, _InputIterator, _Function) [with
_InputIterator = std::_Rb_tree_iterator<std::pair<const
std::basic_string<char, std::char_traits<char>, std::allocator<char>
>, std::pair<int, int> > >, _Function =
std::binder1st<std::mem_fun1_ref_t<void, delay_analyzer,
std::pair<std::basic_string<char, std::char_traits<char>,
std::allocator<char> >, std::pair<int, int> >&> >]’:
msr.cpp:106: instantiated from here
/usr/include/c++/4.2/bits/stl_algo.h:159: error: no match for call to
‘(std::binder1st<std::mem_fun1_ref_t<void, delay_analyzer,
std::pair<std::basic_string<char, std::char_traits<char>,
std::allocator<char> >, std::pair<int, int> >&> >) (std::pair<const
std::basic_string<char, std::char_traits<char>, std::allocator<char>
>, std::pair<int, int> >&)’

zdravko.monov

10/8/2008 4:09:00 PM

0

I also tried to use the ::value_type map's typedef. No success. I
wonder what is wrong here...

Eric.Malenfant@gmail.com

10/8/2008 5:58:00 PM

0

On 8 oct, 11:56, khalderon <zdravko.mo...@gmail.com> wrote:
> > On 8 out, 08:04, khalderon <zdravko.mo...@gmail.com> wrote:
>
> > > I have a map and want to invoke an object member function over each
> > > element:
>
> > > typedef map<string, pair<int, int> > table_t;
> > > table_t table;
>
> > > class delay_analyzer {
> > >         void calculate_conn(pair<string, pair<int, int> >&);
>
> > > void delay_analyzer::process()
> > > {
> > >         for_each
> > >         (
> > >                 table.begin(), table.end(),
> > >                 bind1st
> > >                 (
> > >                         mem_fun_ref(&delay_analyzer::calculate_conn, this)
> > >                 )
> > >         );
>
> > > }
> > > };
>
[snip]
> I am afraid this was not the only problem. I get these errors:
[snip]
> /usr/include/c++/4.2/bits/stl_function.h:405: error: forming reference
> to reference type ‘std::pair<std::basic_string<char,

std::bind1st::operator() passes a const& to the bound function's
second_argument_type, which is a reference. So it ends up trying to
pass a reference to a reference, which is illegal.

One solution: use boost::bind (or, I guess, tr1::bind):
std::for_each(table.begin(), table.end(), boost::bind(
&delay_analyzer::calculate_conn, *this, _1
));

HTH,

Éric Malenfant