473,320 Members | 1,823 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Invoking a member function over the elements in a map

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<ch ar,
std::char_traits<char>, std::allocator<char, std::pair<int, int>
>&), delay_analyzer* const)’
Any ideas what to do?
Oct 8 '08 #1
5 3205
On 8 out, 08:04, khalderon <zdravko.mo...@gmail.comwrote:
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<ch ar,
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
Oct 8 '08 #2
oops. I must have made a typo. When I return home, I will give it a
try...
Oct 8 '08 #3
On Oct 8, 2:55*pm, Leandro Melo <ltcm...@gmail.comwrote:
On 8 out, 08:04, khalderon <zdravko.mo...@gmail.comwrote:
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<ch ar,
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<_Operationstd::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&)’
Oct 8 '08 #4
I also tried to use the ::value_type map's typedef. No success. I
wonder what is wrong here...
Oct 8 '08 #5
On 8 oct, 11:56, khalderon <zdravko.mo...@gmail.comwrote:
On 8 out, 08:04, khalderon <zdravko.mo...@gmail.comwrote:
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
Oct 8 '08 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Elven | last post by:
Hi! I've been trying to figure this out, but can't seem to find an answer. Most likely it's some weird bug in my code, but since I don't have access to the Standard, I wanted to cover my bases.....
3
by: jjleto | last post by:
I have just learn about "pointer to member". Does someone have an actual example where "pointers to member" are usefull or necessary ? Can't always &(myClass.member) be used ? Thank you.
1
by: Steven T. Hatton | last post by:
I have a std::vector<Vec3*> _vertices and an object BoundingBox _bbox. _bbox has a member function called void expandBy(Vec3& v); I want to call _bbox.expandBy on every member of _vertices. I...
10
by: Adam Warner | last post by:
Hi all, With this structure that records the length of an array of pointers as its first member: struct array { ptrdiff_t length; void *ptr; };
22
by: ypjofficial | last post by:
Is there any possibility of invoking the member functions of a class without creating an object (or even a pointer to ) of that class. eg. #include <iostream.h> class test { public: void...
9
by: gk245 | last post by:
I have something like this: struct block { int x; int y; float z; }
4
by: ma740988 | last post by:
Trying to build on something I picked up from a Scott Meyers. Consider: class BaseMenuItem { public: virtual void invoke() const = 0; }; template<class Class, class MemFuncPtr, class...
7
by: Bala L | last post by:
I have a class with a private array data member 'm_array'. I have written a public function, called 'fileRead', to read values into the array from a file. I just noticed that I have declared this...
3
by: Ramesh | last post by:
Hi, I am trying to create an array of pointers to member functions inside my class. When I created a global array of type pfn & initialized with member functions and copy it back to the member...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.