pointers to functions problem

This is a discussion on pointers to functions problem within the C++ Programming forums, part of the General Programming Boards category; Kinda urological for my right now why this isn`t working. Code: #include <iostream> using namespace std; void myFunction(void *CallThis, int ...

  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
    20,968
    I believe your function should be:
    Code:
    void myFunction(void (*CallThis)(), int a);
    A void* is not a function pointer.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,334
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

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

  5. #5
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,334
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  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, 01: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, 07:54 AM
  4. pointers, functions, parameters
    By sballew in forum C Programming
    Replies: 3
    Last Post: 11-11-2001, 09:33 PM
  5. Pointers, arrays , functions
    By sballew in forum C Programming
    Replies: 19
    Last Post: 09-16-2001, 11:12 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21