Function Pointer assignment cannot convert types

Has anyone seen this issue? I am not sure how to correct it?

Thank you!

class MainClass {
private:

typedef bool (MainClass::*FunctionPointerType)(int&);

bool functionImplementation(int& a) {
a++; // Just increments the parameter
}
FunctionPointerType getFunctionPointer() {
return functionImplementation;
/* ^^^^^^^^^^^^^^^^^^^^^^^^^^^
error: cannot convert 'MainClass::functionImplementation' from type 'bool (MainClass::)(int&)' to type 'MainClass::FunctionPointerType {aka bool (MainClass::*)(int&)}'
*/
}


public:
void run() {
FunctionPointerType t = MainClass::getFunctionPointer;
/* ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
error: cannot convert 'MainClass::getFunctionPointer' from type 'bool (MainClass::* (MainClass::)())(int&)' to type 'MainClass::FunctionPointerType {aka bool (MainClass::*)(int&)}'
*/
^

}
};
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
	bool functionImplementation (int& a)
	{
		a++; // Just increments the parameter
		return true; //you promised to return something, then you ought to return something
	}

	FunctionPointerType getFunctionPointer ()
	{
		return &MainClass::functionImplementation; //taking the address
	}

	public:
	void run ()
	{
		FunctionPointerType t = this->getFunctionPointer(); //calling the function, `this' is optional
	}
Thank you so much for your help! This definitively worked!!

I was getting a little frustrated because the message wasn't really helping me a lot. Now I understand the underlying logic behind this.

ne555 huge thanks for your help.
Topic archived. No new replies allowed.