Thread: pointers to functions problem

  1. #1
    Registered User
    Join Date
    Jul 2007
    Posts
    88

    pointers to functions problem

    Kinda urological for my right now why this isn`t working.

    Code:
    #include <iostream>
    
    using namespace std;
    
    void myFunction(void *CallThis, int a);
    void myCallback();
    int main();
    
    void myFunction(void *CallThis, int a) {
    	cout << __FUNCDNAME__ << endl;
    	cout << CallThis << endl;
    	CallThis(); //compile error: error C2064: term does not evaluate to a function taking 0 arguments
    
    }
    
    void myCallback() {
    	cout << __FUNCDNAME__ << endl;
    }
    
    int main()
    {
    	void (*ptr)();
    	ptr = myCallback;
    
    	ptr(); //working
    
    	myFunction(ptr, 2);
    
    	return EXIT_SUCCESS;
    }

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    I believe your function should be:
    Code:
    void myFunction(void (*CallThis)(), int a);
    A void* is not a function pointer.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Well you got the type wrong in the function call.

    Say
    void myFunction( void (*CallThis)(), int a)
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #4
    Registered User
    Join Date
    Jul 2007
    Posts
    88
    Also doesn`t work.

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Thanks for the ultra informative feedback.

    However, this works for me, so I've no idea how you cocked it up.
    Code:
    #include <iostream>
    
    using namespace std;
    
    #define __FUNCDNAME__ __func__
    
    void myFunction(void (*CallThis)(), int a);
    void myCallback();
    int main();
    
    void myFunction(void (*CallThis)(), int a) {
    	cout << __FUNCDNAME__ << endl;
    	cout << CallThis << endl;
    	CallThis(); 
    }
    
    void myCallback() {
    	cout << __FUNCDNAME__ << endl;
    }
    
    int main()
    {
    	void (*ptr)();
    	ptr = myCallback;
    
    	ptr(); //working
    
    	myFunction(ptr, 2);
    
    	return EXIT_SUCCESS;
    }
    
    $ g++ foo.cpp
    $ ./a.exe
    myCallback
    myFunction
    1
    myCallback
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  6. #6
    Registered User
    Join Date
    Jul 2007
    Posts
    88
    Quote Originally Posted by Salem View Post
    Thanks for the ultra informative feedback.
    Sorry.

    No idea what I have messed up.

    Thanks, working fine now.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem with pointers
    By jcafaro10 in forum C++ Programming
    Replies: 4
    Last Post: 01-27-2009, 02:36 PM
  2. Some help with class member functions and pointers
    By k_rock923 in forum C++ Programming
    Replies: 1
    Last Post: 07-21-2005, 12:28 AM
  3. Problem with two set of functions..
    By zahid in forum C Programming
    Replies: 7
    Last Post: 12-04-2002, 08:54 AM
  4. pointers, functions, parameters
    By sballew in forum C Programming
    Replies: 3
    Last Post: 11-11-2001, 10:33 PM
  5. Pointers, arrays , functions
    By sballew in forum C Programming
    Replies: 19
    Last Post: 09-16-2001, 11:12 PM