未解决的外部符号,与自制代表 [英] Unresolved external symbol, with home-made delegate

查看:322
本文介绍了未解决的外部符号,与自制代表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个函数 Foo ,它以一个双参数函数作为参数:

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

但是,我想传入的函数类型( func )只需要一个参数 *

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

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

在C#中,我会做:

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

我试图做一个类似于委托类的东西有一个指向绑定成员函数的指针,我已经切换到静态):

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

我使用的是这样:

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

这是目前给我一个链接器错误: unresolved外部符号private:static void(__stdcall * Delegate :: _ Callback)(int)(?_Callback @ Delegate @@ 0P6GXHHPAN0 @ ZA)



<
  • 问题2:有更好的方法吗?



  • *函数typedef包含 __ stdcall ,因为它是从传入的to)C#)

    解决方案

    正如我在我的评论中指出的, ve在类实例中包装回调函数,但由于回调函数是一个原始函数指针,它不能从它获取的类实例访问任何状态。它只能访问静态成员变量,这意味着 all Delegate 实例每当一个新的 Delegate func ,我会使用一个更简单的接口



    例如:

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


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

    解决方案

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

    这篇关于未解决的外部符号,与自制代表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆