2012-08-23 40 views
0

首先...我已經在一個文件下面的代碼名爲core_ns.hC++警告typedef void(* somename)(arg1,arg2);

/* 
* 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空隙(*)(CORE_NS_Connection_Type,CORE_NS_Connection_Type)」
MY_PROJECT/DisplayParameterServer
ParameterServerCSC.cpp

im使用LynxOS178-2.2.2/GCC C++ compilier轉換。我的解決方案隨此警告生成。我想了解警告的含義。任何有識之士都會被讚賞。

+1

成員函數與非成員函數不兼容,是它的要點。 – GManNickG

+0

在編譯器實現中看起來像一個缺陷。編譯器應該發出錯誤而不是警告(IIRC GCC通常會這樣做)。正如@ ecatmur的回答指出,函數指針類型實際上是不兼容的。 –

回答

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參數將被垃圾回收。

+0

這樣做 - 謝謝! – Lorentz

相關問題