Skip to main content
gamedev.net
🔒 Locked

Function pointer error

Started by Taha Ansari Jul 4, 2007 at 7:19 AM 4 replies 550+ views
Original Post
Taha Ansari
Taha Ansari
Greetings! I have the following code:

// testdrv.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <fstream>
#include <iostream>

using namespace std;

typedef void (*FPTR)(void*);

class A
{
public:
	void doSomething(void* data)
	{
	}
};

class X
{
public:
	void call(FPTR func)
	{
		func(NULL);
	}
};

int main(int argc, char* argv[])
{
	X x;
	A a;
	x.call( a.doSomething );
	return 0;
}

which results in the following error: D:\Program Files\testdrv\testdrv.cpp(35) : error C2664: 'call' : cannot convert parameter 1 from 'void (void *)' to 'void (__cdecl *)(void *)' I've tried multiple options like type-casting, /GD and /GR compiler options (enable and disable etc), but am quite frustrated as I cannot get to resolve the problem. Any clue what the error is? Thanks.. Taha.
Evil Steve
Evil Steve
void doSomething(void* data) is a member function and FPTR doesn't declare a member function pointer, just a "normal" function pointer.

You'll either have to use a pointer-to-member-function (Eww) or make doSomething static or global.
Taha Ansari
Taha Ansari
Greetings!

ok, I'm now using a pointer to a member function. code looks as follows:
// testdrv.cpp : Defines the entry point for the console application.//#include "stdafx.h"#include <fstream>#include <iostream>using namespace std;class A{public:	void doSomething(void* data)	{	}};typedef void (A::*SOMECLASSMEMFUNC)(void*);class X{public:	void call(SOMECLASSMEMFUNC func)	{		func(NULL);	}};int main(int argc, char* argv[]){	X x;	A a;	x.call( a.doSomething);	return 0;}


now i'm getting following error:

.....testdrv.cpp(29) : error C2064: term does not evaluate to a function

How should i resolve this? its plain code, in my view.

Evil Steve
Evil Steve
Since func is a pointer to a member function, you need an object to invoke it on. E.g.:
void call(A& obj, SOMECLASSMEMFUNC func){   (obj.*func)(NULL);}
ToohrVyk
ToohrVyk
As a side note, a.doSomething is, conceptually, a synonym for A::doSomething, it does not mean "The function doSomething of instance a" but rather "The function doSomething defined in class A".
Taha Ansari
Taha Ansari
Quote:
Original post by Evil Steve
Since func is a pointer to a member function, you need an object to invoke it on. E.g.:
void call(A& obj, SOMECLASSMEMFUNC func)
{
(obj.*func)(NULL);
}


Quote:
Original post by ToohrVyk
As a side note, a.doSomething is, conceptually, a synonym for A::doSomething, it does not mean "The function doSomething of instance a" but rather "The function doSomething defined in class A".

Greetings!

Thats perfectly what I understand I would need to come up with the solution! The only problem is: class that calls the function pointer (in class X) is supposed to be generic, whereas, 'any' other class in the whole solution/workspace can create X's object and call its 'call()' function, so I will not be able to bound X's call() to take A's reference in it - as it will then no longer remain generic.

To overcome this, what I can think of is: every class that creates an instance of 'X' *must* inherit from a base class: 'Base'. Base should have doSomehting() as a pure virtual function. And X's call would have reference/pointer to Base in its call function: just like you've said. Then every class that wants to create and use 'X' would have to implement doSomething() as it desires.

Thats the one solution I can think of, but this would require changing every said class etc.

But I dont know if its a very good design, or if there is a better solution in the given scope?

Ideas?

Thanks for the help!

Taha

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.