C ++警告到typedef void(* somename)(arg1,arg2);

时间:2012-08-23 17:13:34

标签: c++ typedef void-pointers

首先......我在名为core_ns.h的文件中有以下代码

/*
 * the prototype for the function that will be called when a connection
 * is made to a listen connection.  
 * Arguments:
 *    Server_Connection - ID of the listen connection that received the request
 *    New_Connection    - ID of the data connection that was created.
 */
typedef void (* CORE_NS_Connect_Callback)
                (CORE_NS_Connection_Type  Server_Connection,
                 CORE_NS_Connection_Type  New_Connection);

然后我在名为ParameterServerCSC.h的文件中包含以下内容

class ParameterServer{
public:
    //-------------------------------------------------------------------------------
    //FUNCTION: sendCallback
    //
    //PURPOSE: This method will be performed upon a connection with the client. The 
    //Display Parameter Server will be sent following a connection.
    //-------------------------------------------------------------------------------
    void sendCallback (CORE_NS_Connection_Type serverConnection, CORE_NS_Connection_Type newConnection);
}; // end class ParameterServer

最后......我在名为ParameterServer.cpp

的文件中有以下用法
       //-------------------------------------------------------------------------------
      //FUNCTION: setup 
      //
      //PURPOSE: This method will perform any initialization that needs to be performed
      //at startup, such as initialization and registration of the server. 
      //-------------------------------------------------------------------------------
      void ParameterServer::setup(bool isCopilotMfd){

            CORE_NS_Connect_Callback newConnCallback; 
            newConnCallback = &ParameterServer::sendCallback;//point to local function to handle established connection.
      }

我收到以下警告:

  

警告:转换自void (ParameterServer::*)(CORE_NS_Connection_Type, CORE_NS_Connection_Type)' to void(*)(CORE_NS_Connection_Type,CORE_NS_Connection_Type)'   MY_PROJECT / DisplayParameterServer
  ParameterServerCSC.cpp

我使用的是LynxOS178-2.2.2 / GCC C ++编译器。我的解决方案以此警告为基础。我试图理解警告的含义。对此有任何见解表示赞赏。

1 个答案:

答案 0 :(得分:4)

ParameterServer::sendCallback是成员函数或方法(其类型为void (ParameterServer::*)(CORE_NS_Connection_Type, CORE_NS_Connection_Type))因此不能用作函数(类型void (*)(CORE_NS_Connection_Type, CORE_NS_Connection_Type))。

您需要将其设为静态成员函数:

static void sendCallback (CORE_NS_Connection_Type serverConnection, CORE_NS_Connection_Type newConnection);

否则(取决于调用约定)当API调用sendCallback时,参数将被错误地设置并且将显示为不正确;至少隐藏的this参数将是垃圾。