473,811 Members | 3,479 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 4906
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(c onst 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
2753
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 "clear-C-code". I used <vector>, <algorithm>, <iostream> and <algorithm>. Is STL really so slow? Thx in adv. Przemo
9
1895
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. Here's the question: I want to be able to change the references (including deleting them). Is there any way to do that besides using pointers rather than references for the STL library? I'd also prefer to avoid using const_cast, if it is indeed...
13
1893
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 5.0-0409 with vc 6.0. How can I extend STL easily? Any help will be appreciated. Thanks.
3
1829
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 while compilation, but do not know why its throwing errors... Compiling testStl.cpp /grid/common/pkgs/gcc/v3.2.3p1/include/c++/3.2.3/bits/locale_facets.tcc: In member function `virtual _InIter std::num_get<_CharT, _InIter>::do_get(_InIter,...
12
1618
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 mp); STRMAP *mp; //unmamaged object
7
1993
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 main(array<System::String ^> ^args)
3
9869
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 determine if the one item is less than the other (it uses two vectors, one containing the actual data, and one that stores IDs that index into the data vector). The code below is pretty self-explanatory. I've looked into mem_fun, but I'm stuck....
8
1883
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: template <class charT, class traits = char_traits<charT class basic_ostream : virtual public basic_ios<charT,traits>
7
3134
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 "myList" as in the example below ? #include "Sector.h" using namespace boost;
0
9731
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9605
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10651
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10393
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9208
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7671
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5556
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5697
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3871
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.