简体   繁体   中英

Unresolved external symbol, with home-made delegate

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#)

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 .

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;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM