| | |
How to call a class member function via function pointer in map [C++]
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Jul 2006
Posts: 35
Reputation:
Solved Threads: 0
I'm trying to declare a std::map that is keyed by a char and has a function pointer as a second parameter, these functions would be a member function of my class. But for some odd reason I cannot call the functions when extracted...
So, I have a class A as shown below, it contains a map as well as a function
Next I have main code that looks like this:
Now, I try to execute func by extracting it from the map and running the function:
I would assume this would launch function "func" as it is stored in the map, but instead it generates the following error message:
error C2064: term does not evaluate to a function taking 0 arguments
Any help would be greatly appreciated...
Thanks,
So, I have a class A as shown below, it contains a map as well as a function
C++ Syntax (Toggle Plain Text)
class A { private: map<char, void (A::*)(void)> mapA; public: void func();
Next I have main code that looks like this:
C++ Syntax (Toggle Plain Text)
A::A() // constructor { // Generate User Options mapA['a'] = &A::func; }
Now, I try to execute func by extracting it from the map and running the function:
C++ Syntax (Toggle Plain Text)
map<char, void (A::*)(void)>::iterator it; for ( it=mapA.begin() ; it != mapA.end(); it++ ) { (*it).second(); }
I would assume this would launch function "func" as it is stored in the map, but instead it generates the following error message:
error C2064: term does not evaluate to a function taking 0 arguments
Any help would be greatly appreciated...
Thanks,
•
•
Join Date: Dec 2007
Posts: 360
Reputation:
Solved Threads: 69
Try boost::function -> http://www.boost.org/doc/libs/1_39_0.../function.html
Last edited by jencas; Jul 6th, 2009 at 5:13 am. Reason: link added
If you are forced to reinvent the wheel at least try to invent a better one!
Please use code tags - Please mark solved threads as solved
Please use code tags - Please mark solved threads as solved
•
•
Join Date: Jun 2006
Posts: 147
Reputation:
Solved Threads: 20
try the following.
the syntax seems difficult but look it carefully.
the easier way is to create the typedef and assign the return value of m['a'] to that, and then call that pointer to member function.
like below.
same goes for iterator.
hope this helps
the syntax seems difficult but look it carefully.
C++ Syntax (Toggle Plain Text)
(f.*((*it).second))(); (f.*(it->second))();
the easier way is to create the typedef and assign the return value of m['a'] to that, and then call that pointer to member function.
like below.
C++ Syntax (Toggle Plain Text)
typedef void (A::*FMemPtr)(void); map<char, FMemPtr> m; m['a'] = &A::func; FMemPtr funcPtr = m['a']; A obj; (obj.*funcPtr)();
hope this helps
•
•
Join Date: Jun 2006
Posts: 147
Reputation:
Solved Threads: 20
![]() |
Similar Threads
- errorcode C2352 - illegal call of a non static member function. (C++)
- DialogBox() function and function pointer in class scope (C++)
- How to get adress of class function. (Function pointer) (C++)
- calling oo class member function from c (C)
- How to read data from csv file in an array and parse (C++)
- performance benefit by not calling static member function by object (C)
Other Threads in the C++ Forum
- Previous Thread: please help
- Next Thread: C++ General Tree
| Thread Tools | Search this Thread |
api array based binary bitmap c++ c/c++ calculator char char* class classes code coding compile console conversion count database delete deploy desktop developer directshow dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game givemetehcodez google graph gui homeworkhelp iamthwee ifstream input int java lib linkedlist linker linux list loop looping loops map math matrix memory multiple news node number numbertoword output pointer problem program programming project python random read recursion recursive reference return rpg sorting string strings temperature template templates test text text-file tree unix url variable vector video visualstudio win32 windows winsock word wordfrequency wxwidgets







.