Invoking a member function over the elements in a map

K

khalderon

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,
&), delay_analyzer* const)’

Any ideas what to do?
 
L

Leandro Melo

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,


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...
 
K

khalderon

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...

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,
/usr/include/c++/4.2/bits/stl_function.h:411: error: forming reference
to reference type ‘std::pair<std::basic_string<char,
/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::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>,
 
K

khalderon

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

Eric.Malenfant

[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::eek:perator() 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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top