Error Taking Address Of The Bound Function
here for a quick overview of the site Help Center Detailed answers to any questions you might have Meta Discuss the workings and policies of this
Bound Member Function
site About Us Learn more about Stack Overflow the company Business Learn iso c++ forbids taking the address of a bound member function more about hiring developers or posting ads with us Stack Overflow Questions Jobs Documentation Tags Users Badges Ask Question
Iso C++ Forbids Taking The Address Of An Unqualified Or Parenthesized Non-static Member Function
x Dismiss Join the Stack Overflow Community Stack Overflow is a community of 4.7 million programmers, just like you, helping each other. Join them; it only takes a minute: Sign up Passing c++ bind member function member function pointer to member object in c++ up vote 15 down vote favorite 11 I have a problem with using a pointer to function in C++. Here is my example: #include using namespace std; class bar { public: void (*funcP)(); }; class foo { public: bar myBar; void hello(){cout << "hello" << endl;}; }; void byebye() { cout << "bye" << endl; c++ pointer to member function example } int main() { foo testFoo; testFoo.myBar.funcP = &byebye; //OK testFoo.myBar.funcP = &testFoo.hello; //ERROR return 0; } Compilator returns an error at testFoo.myBar.funcP = &testFoo.hello;: ISO C++ forbids taking the address of a bound member function to form a pointer to member function. Say '&foo::hello' cannot convert 'void (foo::)()' to 'void ()()' in assignment So i tried it like this: class bar { public: void (*foo::funcP)(); }; But now the compilator adds one more: 'foo' has not been declared Is there a way make it work? Thanks in advance for suggestions c++ function-pointers member-function-pointers share|improve this question asked Mar 3 '10 at 20:50 Moomin 63931536 add a comment| 4 Answers 4 active oldest votes up vote 11 down vote accepted Taking everyone's suggestions together, your final solution will look like: #include using std::cout; usind std::endl; class foo; // tell the compiler there's a foo out there. class bar { public: // If you want to store a pointer to each type of function you'll // need two different pointers here: void (*freeFunctionPointer)(); void (foo::*memberFunctionPointer)(); }; class foo { public: bar myBar; void hello(){ cout << "hello" << endl
1 of 1 New Topic/Question Reply 7 Replies - 5469 Views - Last Post: 14 April 2014
Std::bind
- 05:32 AM Rate Topic: #1 IceHot D.I.C Head Reputation: boost::bind 0 Posts: 220 Joined: 28-August 12 The correct way to pass member function pointers Posted
C++ Functor
13 April 2014 - 07:23 PM I have a class called SortingTest (yes, I also have that habit of using OOP), in which I am trying http://stackoverflow.com/questions/2374847/passing-member-function-pointer-to-member-object-in-c to use a function pointer to specify the sorting algorithm used. Namely I have, in my header file, typedef void (*SortingAlgorithm)(Array&); and the following function that uses function pointer in my .cpp file: void SortingTest::startAndTime(SortingAlgorithm algorithm, OutputStream& stream) { // same as the function above, but use the stream Array array(this->arrayToSort); // making http://www.dreamincode.net/forums/topic/344696-the-correct-way-to-pass-member-function-pointers/ copy of arrayToSort so that it is unmodified by whatever we do to the copy stream << "Sorting array...\n"; clock_t t = clock(); algorithm(array); stream << "Array sorted...\n"; t = clock() - t; stream << "It took me " << ((double)t)/CLOCKS_PER_SEC << " seconds.\n"; } I think this is the way to go, but when I try to define a function that calls this method for all of the sorting functions I have implemented: void SortingTest::startAndTimeAllAlgorithms(OutputStream& stream) { stream << "Now testing heapSort...\n\n"; this->startAndTime((SortingAlgorithm) &(this->heapSort), stream); stream << "\n\nNow testing mergeSort...\n\n"; this->startAndTime(this->mergeSort(Array&), stream); stream << "\n\nNow testing quickSort...\n\n"; this->startAndTime(this->quickSort(Array&), stream); stream << "\n\nNow testing heapSort...\n\n"; this->startAndTime(this->heapSort(Array&), stream); } I get the following errors: SortingTest.cpp: In member function âvoid SortingTest::startAndTimeAllAlgorithms(OutputStream&)â: SortingTest.cpp:327: error: ISO C++ forbids taking the address of a bound member function to form a pointer to member function. Say â&SortingTest::heapSortâ SortingTest.cpp:327: warning: converting from âvoid (SortingTest::*)(Array&)â to âvoid (*)(Array&)â SortingTest.cpp:329: error:
and get tips & solutions from a community of 418,546 IT Pros & Developers. It's https://bytes.com/topic/c/answers/622298-pointer-member-function quick & easy. Pointer to member function? P: 1 pheres Hi, I'm trying to pass pointers to member functions around in my code. The test with pointers to non-member function works fine: Expand|Select|Wrap|Line Numbers Code: voidcallOut(void(*callback)()) { callback(); } voidtestOut() { cout<<"testOuthier"<*’tocallpointer-to-member //functionin‘callback(...)’ } intmain(intargc,char**argv) { A*a=newA; callOut(&a->out); //error:ISOC++forbidstakingtheaddressofabound //memberfunctiontoformapointerto //memberfunction.Say‘&A::out’ } So gcc says it's impossible to use pointers to member function. But I found some tutorials talking about them (but didn't understood them ) Is it possible? And if bound member function yes could you please correct my example code? Background is I'm implementing a finite state machine and the output function of the states are virtual methods of subclasses of an ABC, so pointers to member functions (of the correct derived type) seemed to do the job. If you say pointer to member fuctions doesn't work in C++, do you know some better idiom to use for this purpose? tank you very much! Mar 25 '07 #1 Post Reply Share this Question 1 Reply Expert 100+ P: 1,510 horace1 try Expand|Select|Wrap|Line Numbers //AmemberFuncpointstoamemberofAthattakes() typedefvoid(A::*AmemberFunc)(); //passreferencetoAandpointertomemberfunction voidcallOut(A&a,AmemberFuncpFunc) { (a.*pFunc)();//callmemberfunction } and calling it so Expand|Select|Wrap|Line Numbers Aa; callOut(a,&A::out); have a read thru http://www.parashift.com/c++-faq-lite/pointers-to-members.html Mar 25 '07 #2 reply Message Cancel Changes Post your reply Join Now >> Sign in to post your reply or Sign up for a free account. Similar topics Function pointer help Functionoids vs Member Function Pointer? Write Function Pointer Array Pointer to non-static member functions Member function pointer cast question passing void function pointer Lif