Thread: Problem with function pointers

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Banned
    Join Date
    May 2004
    Posts
    129

    Problem with function pointers

    I have this setup:

    I have a class, and one of the members is a function pointer. I want to be able to implement various possible versions of the function it points to in the same class the function pointer resides in. However, I get various errors when I try to initialize the function pointer to a function that also is a member of the same class.

    I've shown the first part, then the error it generates, then in the second part I try to remedy it by declaring the function pointer to be a member of the Vec class, but it still doesn't work (I show the error codes of visual C++ 6). Of course, I couldn't then put this same function pointer in the baseclass, but it could at least point to a member function (sadly I can't even get this much to work properly).

    Code:
    #include	<iostream>
    using namespace std;
    
    //pFunc is of data type function pointer which returns void, accepts void as formal parameter
    typedef	void(*pFunc)(void);
    
    void	SomeFunc()
    {
    	cout	<<	"This is just some func" << endl;
    }
    
    typedef	struct	
    {
    	Vec()
    	{
    		x = y = z = w = 0;
    		funcPtr	=	0;
    	}
    	void	VectorFunc()
    	{
    		cout	<<	"This is the vector func" << endl;
    	}
    	pFunc	funcPtr;
    	float	x;
    	float	y;
    	float	z;
    	float	w;	//complex plane
    }Vec,*VectorPTR;
    
    typedef	 void(Vec::*VecFuncPtr)(void);
    
    int	main(void)
    {
    	Vec a;
    	a.funcPtr	=	SomeFunc; //works
    	a.funcPtr();
    	
    	a.funcPtr	=	Vec::VectorFunc;	//error:
    	/*
    	error C2440: '=' : cannot convert from 'void (__thiscall Vec::*)(void)' to 'void (__cdecl *)(void)'
    	*/
    
    	VecFuncPtr b;
    	b = Vec::VectorFunc;
    	b();	//error:
    	/*
    		error C2064: term does not evaluate to a function
    	*/
    
    	return 0;
    }
    Last edited by vNvNation; 06-12-2004 at 04:49 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. In over my head
    By Shelnutt2 in forum C Programming
    Replies: 1
    Last Post: 07-08-2008, 06:54 PM
  2. Undefined Reference Compiling Error
    By AlakaAlaki in forum C++ Programming
    Replies: 1
    Last Post: 06-27-2008, 11:45 AM
  3. Class function pointers
    By VirtualAce in forum C++ Programming
    Replies: 40
    Last Post: 02-17-2005, 12:55 AM
  4. pointers
    By InvariantLoop in forum C Programming
    Replies: 13
    Last Post: 02-04-2005, 09:32 AM
  5. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM