Unresolved external symbol, with home-made delegate

心不动则不痛 提交于 2019-12-24 10:19:37

问题


I have a function Foo which takes a 2-parameter function as a parameter:

void Foo(void (*fcn)(int, int*));

However, the type of function which I want to pass in (func) only takes 1 parameter*.

typedef void (__stdcall *FuncCallBack)(int a);

void Caller(FuncCallBack func) {
   Foo(????);
}

In C#, I would do something like:

Foo((a,b) -> func(a));

I'm trying to do something similar with a delegate class (having worked out that I can't have a pointer to a bound member function, I've switched to static):

class Delegate {
private:
 static FuncCallBack _Callback;
public
 Delegate(FuncCallBack);
 static void Callback(int, int*);
}

Delegate::Delegate(FuncCallback callback) { _Callback = callback; }

void Delegate::Callback(int a, int *b) { _Callback(a); }

Which I use like so:

void Caller(FuncCallBack func) {
   Delegate d = Delegate(func);
   Foo(&(d.Callback));
}

This is currently giving me a linker error: unresolved external symbol "private: static void (__stdcall* Delegate::_Callback)(int)" (?_Callback@Delegate@@0P6GXHHPAN0@ZA)

  • Question 1: What could be causing this linker error?
  • Question 2: Is there a better way to do this?

*The function typedef includes __stdcall because it is passed in from (and will be calling back to) C#)


回答1:


As I pointed out in my comment, using a class makes it seem like you've wrapped the callback function in a class instance but as the callback function is a raw function pointer it can't access any state from the class instance that it was obtained from. It can only access static member variables and this means that the behaviour for all Delegate instances whenever a new Delegate instance is constructed.

If you have to support calling different func I would use a simpler interface that makes the fact that only callback function is active at one time more evident from the interface.

For example:

callbackwrapper.h:

void CallbackWrapper(int, int*);
void SetWrappedCallback(void (__stdcall *toWrap)(int));

callbackwrapper.cpp:

namespace {
    void (__stdcall *wrappedCallback)(int);
}

void CallbackWrapper(int a, int*)
{
    wrappedCallback(a);
}

void SetWrappedCallback(void (__stdcall *toWrap)(int))
{
    wrappedCallback = toWrap;
}



回答2:


You need to define the static members of classes, exactly once (in Delegate.cpp for example):

FuncCallBack Delegate::_Callback;

In the posted code there is only a declaration of _Callback.



来源:https://stackoverflow.com/questions/10879518/unresolved-external-symbol-with-home-made-delegate

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!