gpt4 book ai didi

c++ - 指向非静态成员函数的函数指针

转载 作者:太空宇宙 更新时间:2023-11-04 11:29:38 25 4
gpt4 key购买 nike

错误:

C:\testa\game.cpp|147|error: no matching function for call to 'game::register_handler(PacketFamily, PacketAction, 
<unresolved overloaded function type>)'|

这里是部分代码,因为它太大了。

我必须在类游戏中输入定义 handler_callback,否则两者之一对于另一个将是未定义的。

游戏.h

class game
{
typedef bool (game::*handler_callback)(PacketReader reader);
public:
bool default_handler_init (PacketReader reader);
void register_default_handlers();
void register_handler(PacketFamily family, PacketAction action,handler_callback callback);
};

游戏.cpp

 void game::register_default_handlers()
{
register_handler(PACKET_F_INIT, PACKET_A_INIT,default_handler_init);

}

void game::register_handler(PacketFamily family, PacketAction action,handler_callback callback)
{
handlers.insert(std::make_pair(std::make_pair(family, action), callback));
}

最佳答案

我将 OP 的代码扩展为以下内容以便编译:

class PacketReader {};
class PacketFamily {};
class PacketAction {};
const PacketFamily PACKET_F_INIT;
const PacketAction PACKET_A_INIT;

class game
{
typedef bool (game::*handler_callback)(PacketReader reader);
public:
bool default_handler_init (PacketReader reader);
void register_default_handlers();
void register_handler(PacketFamily family, PacketAction action, handler_callback callback);
};

void game::register_default_handlers()
{
register_handler(PACKET_F_INIT, PACKET_A_INIT, default_handler_init);
}

void game::register_handler(PacketFamily family, PacketAction action, handler_callback callback)
{
//handlers.insert(std::make_pair(std::make_pair(family, action), callback));
}

使用命令 g++ -c -Wall -std=c++11 test.cpp 用 g++ 编译会产生以下错误:

test.cpp: In member function ‘void game::register_default_handlers()’:
test.cpp:18:71: error: no matching function for call to ‘game::register_handler(const PacketFamily&, const PacketAction&, <unresolved overloaded function type>)’
register_handler(PACKET_F_INIT, PACKET_A_INIT,default_handler_init);
^
test.cpp:18:71: note: candidate is:
test.cpp:13:10: note: void game::register_handler(PacketFamily, PacketAction, game::handler_callback)
void register_handler(PacketFamily family, PacketAction action,handler_callback callback);
^
test.cpp:13:10: note: no known conversion for argument 3 from ‘<unresolved overloaded function type>’ to ‘game::handler_callback {aka bool (game::*)(PacketReader)}’

无论如何,要解决这个问题,请将 &game:: 添加到对 register_handler 的调用中:

register_handler(PACKET_F_INIT, PACKET_A_INIT,&game::default_handler_init);

关于c++ - 指向非静态成员函数的函数指针,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25299336/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com