• 0

Function to function as pointer unknow problem


Question

hello

i needed to make a callback function, i read a lots of articles and created this (i used this principe on simple code, and added it to my program)

Code:

class TKeyboard {
	public :
		...
		void setWhilePressedCallback(void (*func) (int));
	private :
		...
};

void setWhilePressedCallback(void (*func) (int)) {
	...
}

but now iam getting this error after i call

keyboard.setWhilePressedCallback(test); /*void test(int x) {}*/

1>camera.obj : error LNK2019: unresolved external symbol "public: void __thiscall TKeyboard::setWhilePressedCallback(void (__cdecl*)(int))" (?setWhilePressedCallback@TKeyboard@@QAEXP6AXH@Z@Z ) referenced in function _main

1>F:\Work\Visual Studio\my\camera\Debug\camera.exe : fatal error LNK1120: 1 unresolved externals

the full source is at

politea.sk/tmp/camera.rar

THANK YOU FOR HELP

Link to comment
Share on other sites

4 answers to this question

Recommended Posts

  • 0

Fixed.

class TKeyboard {
public :
	...
	void setWhilePressedCallback(void (*func) (int));
private :
	...
};

void TKeyboard::setWhilePressedCallback(void (*func) (int)) {
	...
}

Edited by Dr_Asik
Link to comment
Share on other sites

  • 0
Fixed.

class TKeyboard {
public :
	...
	void setWhilePressedCallback(void (*func) (int));
private :
	...
};

void TKeyboard::setWhilePressedCallback(void (*func) (int)) {
	...
}

OUU THANK YOU

i mean, i was searching the error everywhere, and i didnt notice that it was all the time infront of me, and just such a stupid one

i dont really like the error output of MVS :( i migrated from delphi -> borland c++ -> MVS but delphi and borland c++ had much much better debuging capabilities

now iam passing the test function to the callback function, what do i need to do, to pass smt like "camera.turnLeft" so a function that belongs to a class?, like

keyboard.setWhilePressedCallback(camera.turnLeft)

i know i should define it than like void (TCamera::*func) (int)

but isnt there another option to pass the camera.turnLeft but dont declare the function with TCamera::* ?

Link to comment
Share on other sites

  • 0

You can use templates to make the parameter generic, so it can be any type of function pointer. The downside is that now the parameter can be anything, not just function pointers, so if the user passes an int and you try to call that it'll crash. To get both the flexibility of templates and limit the permitted types to function pointers, you can use partial template specialization. The syntax is very touchy so I don't have the time to produce a working example but hopefully you can google for that.

And if you think what you've seen of error messages is obscure, wait until you have to deal with template error messages. You often get virtually no relevant information at all, or completely absurd messages like "Cannot convert from type Something to type Something". It's a trip back to the 1980s where C++ compilers had one error message : "Error." :D

Edited by Dr_Asik
Link to comment
Share on other sites

  • 0
You can use templates to make the parameter generic, so it can be any type of function pointer. The downside is that now the parameter can be anything, not just function pointers, so if the user passes an int and you try to call that it'll crash.
Just to rectify something I said earlier. It won't crash at run-time, you'll get a compiler error before that, but it'll be pretty obscure. With partial specialization you can still be a lot more precise about what you want.
Link to comment
Share on other sites

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.