Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/130.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 无法将函数指针作为函数参数传递_C++_Function Pointers_Member Function Pointers_Pointer To Member - Fatal编程技术网

C++ 无法将函数指针作为函数参数传递

C++ 无法将函数指针作为函数参数传递,c++,function-pointers,member-function-pointers,pointer-to-member,C++,Function Pointers,Member Function Pointers,Pointer To Member,我想将函数指针作为函数参数传递 这是我的密码: void AuthServerOpcodes::ValidateAndSetServerOpcode(ServerOpcode serverOpcode, void(*handlerFunc(std::vector<std::byte> data))) {} 以下是我如何通过考试的: ValidateAndSetServerOpcode(SMSG_LOGIN_REQUEST, &Test); 然而,这似乎不是正确的方法。当我

我想将函数指针作为函数参数传递

这是我的密码:

void AuthServerOpcodes::ValidateAndSetServerOpcode(ServerOpcode serverOpcode, void(*handlerFunc(std::vector<std::byte> data))) {}
以下是我如何通过考试的:

ValidateAndSetServerOpcode(SMSG_LOGIN_REQUEST, &Test);
然而,这似乎不是正确的方法。当我试图以这种方式进行操作时,会出现以下错误:

Cannot initialize a parameter of type 'void (*(*) 
(std::vector<std::byte>))' with an rvalue of type 'void 
(AuthServerOpcodes::*)(std::vector<std::byte>)': different return type 
('void (*)' vs 'void')
无法初始化类型为“void(*(*)的参数
(std::vector)),其右值类型为“void”
(AuthServerOpcodes::*)(std::vector)“”:不同的返回类型
('void(*)与'void')
这是为什么?我该如何修复它?

你不能这么做

没有指向该函数的函数指针,因为它是一个成员函数


您可以将指针传递给成员函数,或者更好的是,将
std::function
绑定到捕获
指针的lambda。

这只是类型不匹配,您的函数是AccountManager类的方法, 所以它的签名类似于:
静态无效登录(AccountManager*this,std::vector data)

您可以将函数从类中分离出来,更改您的代码定义<代码> HooLrFunc 或考虑不同的技术,如:代码> STD::MimIfFN或<代码> STD:BION/COD> < /P>


指向成员的指针必须使用类类型限定,因此您需要获取需要使用的指针

ValidateAndSetServerOpcode(SMSG_LOGIN_REQUEST, &AuthServerOpcodes::Test);
但看起来您在上一次编辑中已经尝试过了,所以我猜您错误地调用了指向成员的函数指针。你没有显示一个,所以我不能帮你更多,请创建一个。无论如何,我已经在编译器资源管理器上创建了一个

typedef void (AuthServerOpcodes::*HandlerFunc)(std::vector<std::byte> &);

void AuthServerOpcodes::ValidateAndSetServerOpcode(ServerOpcode serverOpcode,
        HandlerFunc handlerFunc)
{
    std::vector<std::byte> myVector;
    (this->*handlerFunc)(myVector);   // call the hander
}

void FreeStandingFunction(AuthServerOpcodes& opc,
    AuthServerOpcodes::HandlerFunc handlerFunc,
    std::vector<std::byte> &data)
{
    (opc.*handlerFunc)(data);
}
typedef void(AuthServerOpcodes::*HandlerFunc)(std::vector&);
void AuthServerOpcodes::ValidateAndSetServerOpcode(ServerOpcode ServerOpcode,
HandlerFunc HandlerFunc)
{
std::vector myVector;
(this->*handlerFunc)(myVector);//调用hander
}
无效独立功能(AuthServerOpcodes和opc,
AuthServerOpcodes::HandlerFunc HandlerFunc,
标准:矢量和数据)
{
(opc.*handlerFunc)(数据);
}
如您所见,指向成员的指针必须使用
->*
*
调用,并且整个取消引用必须包装在
()
中,因为这些运算符的优先级低于函数调用运算符
()

另见

一些离题说明:

  • 不要使用像那样太长的线条
  • 除非确实需要保留外部值,否则不要通过值传递向量。始终通过引用传递
    const std::vector&
    (或删除
    const
    以修改外部变量)

它不应该是
void(*handlerFunc)(std::vector data)
?@nuttracker No。这能回答你的问题吗?因此,我可以传递指向同一类或父类函数的指针?朋友类呢?@AsteroidsWithWigns我已经用您建议的对调用成员函数的更改更新了我的问题,但仍然不起作用。@AsteroidsWithWigns能否请您举一个关于
std::function
的示例,以便答案可能是正确的完整一点,我可以接受吗?你只需花十秒钟的研究就能找到一个。谢谢你的回答!那么,如果我在
AuthServerOpcodes
中创建一个函数,我可以传递它吗?你能用
std::bind
做个例子吗?
typedef void (AuthServerOpcodes::*HandlerFunc)(std::vector<std::byte> &);

void AuthServerOpcodes::ValidateAndSetServerOpcode(ServerOpcode serverOpcode,
        HandlerFunc handlerFunc)
{
    std::vector<std::byte> myVector;
    (this->*handlerFunc)(myVector);   // call the hander
}

void FreeStandingFunction(AuthServerOpcodes& opc,
    AuthServerOpcodes::HandlerFunc handlerFunc,
    std::vector<std::byte> &data)
{
    (opc.*handlerFunc)(data);
}