473,511 Members | 9,983 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

[STL] using foreach in member function

1 New Member
Hello,

I wrote the following class:

class A {
//.. class description
};

class B {
void foo(A* a) {
//.. function description
}

void bar() {
for_each ( H->begin(), H->end(), &B::foo );
}

// member variable
vector<A*> *H; // initialize in elsewhere
}


In this case, the error message was
must use '.*' or '->*' to call pointer-to-member function in '__f (...)'
How can I call member function 'foo' in for_each command?

I aware that if function foo is the member function of class A, mem_fun should be used in for_each argument.
However, in my case, foo is the member function of class B. Is 'mem_fun' should used in this case?

In case of use mem_fun(&B::foo) instead of &B::foo, I got an error message;
argument of type 'void (H::)(A*) does not match 'void (H::*)(A*)'

I appreciate for your comment.
Aug 17 '09 #1
2 4893
JosAH
11,448 Recognized Expert MVP
@church7
The third argument of the for_each template has to be an ordinary function (not a member function) or an object of a class that implements the operator() operator. Add an operator() operator that calls your function foo and you're done.

kind regards,

Jos
Aug 17 '09 #2
unauthorized
81 New Member
As a side note, you can use Boost.Foreach to greatly simplify your code by allowing you to use syntax similar to foreach in other languages.
You could do something like this:
vector<X> my_vect;
BOOST_FOREACH(const X& myvar, my_vect)
{
// do something with myvar, which is element of my_vect
}
Aug 17 '09 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

30
2688
by: Przemo Drochomirecki | last post by:
Hi, The task was indeed simple. Read 2.000.000 words (average length = 9), sort them and write it to new file. I've made this in STL, and it was almost 17 times slower than my previous...
9
1867
by: Aguilar, James | last post by:
Hey guys. A new question: I want to use an STL libarary to hold a bunch of objects I create. Actually, it will hold references to the objects, but that's beside the point, for the most part. ...
13
1864
by: Park Ho-Kyung | last post by:
Hello. I try to extend STL, for example, add member function like trim_left to basic_string<charT, traits, Alloc>. However I am in the despair as soon as I see STL source. I am using STLport...
3
1811
by: pervinder | last post by:
Hi, I am using STL from http://www.stlport.org/download.html (ver 4.6.2) I see hundered's of error while compilation. I have a c++ prog which uses stl. I am providing the stlport in the include -I...
12
1591
by: MuZZy | last post by:
HI, Say i have a class(on C++) which has an unmanaged STL member of STRMAP which is a define for map<string, string>. class CL { public CL(){mp = new STRMAP()}; public void Dispose(delete...
7
1972
by: rodrigostrauss | last post by:
I'm using Visual Studio 2005 Professional, and I didn't find the STL.NET. This code: #include "stdafx.h" #include <vector> using namespace System; using namespace std; int...
3
9846
by: Nelis Franken | last post by:
Good day. I'm having trouble telling STL to use a member function to sort items within the relevant class. The isLess() function needs to have access to the private members of the Foo class to...
8
1866
by: kevin | last post by:
Hello! So I was reading O'Reilly's C++ in a Nutshell when I came accross something interesting: The class definition for basic_ostream contains numerous overloaded operator<< functions: ...
7
3111
by: ademirzanetti | last post by:
Hi there !!! I would like to listen your opinions about inherit from a STL class like list. For example, do you think it is a good approach if I inherit from list to create something like...
0
7251
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7148
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7367
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7430
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
7089
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
4743
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
1581
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
790
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
451
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.