472,809 Members | 2,594 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

[STL] using foreach in member function


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 4843
11,448 Expert 8TB
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,

Aug 17 '09 #2
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

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...
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. ...
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...
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...
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...
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...
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...
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: ...
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...
by: erikbower65 | last post by:
Using CodiumAI's pr-agent is simple and powerful. Follow these steps: 1. Install CodiumAI CLI: Ensure Node.js is installed, then run 'npm install -g codiumai' in the terminal. 2. Connect to...
by: linyimin | last post by:
Spring Startup Analyzer generates an interactive Spring application startup report that lets you understand what contributes to the application startup time and helps to optimize it. Support for...
by: erikbower65 | last post by:
Here's a concise step-by-step guide for manually installing IntelliJ IDEA: 1. Download: Visit the official JetBrains website and download the IntelliJ IDEA Community or Ultimate edition based on...
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Sept 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
by: Rina0 | last post by:
I am looking for a Python code to find the longest common subsequence of two strings. I found this blog post that describes the length of longest common subsequence problem and provides a solution in...
by: DJRhino | last post by:
Private Sub CboDrawingID_BeforeUpdate(Cancel As Integer) If = 310029923 Or 310030138 Or 310030152 Or 310030346 Or 310030348 Or _ 310030356 Or 310030359 Or 310030362 Or...
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
by: lllomh | last post by:
How does React native implement an English player?
by: Mushico | last post by:
How to calculate date of retirement from date of birth

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.