簡體   English   中英

指向非靜態成員函數的函數指針

[英]Function pointers to non static member functions

錯誤:

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

這是代碼的一部分,因為它太大了。

我必須在類游戲中輸入typedef handler_callback,否則兩者之一將無法定義。

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

game.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 ++編譯g++ -c -Wall -std=c++11 test.cpp產生以下錯誤:

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

無論如何,要解決此問題,請在register_handler的調用中添加&game::

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM