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

标签 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.
      }

我收到以下警告:

warning: converting from void (ParameterServer::*)(CORE_NS_Connection_Type, CORE_NS_Connection_Type)' tovoid (*)(CORE_NS_Connection_Type, CORE_NS_Connection_Type)'
MY_PROJECT/DisplayParameterServer
ParameterServerCSC.cpp

我正在使用 LynxOS178-2.2.2/GCC C++ 编译器。我的解决方案基于此警告。我试图理解警告的含义。任何对此的见解都将受到赞赏。

最佳答案

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 参数将是垃圾。

关于C++ 对 typedef void (* somename) (arg1, arg2) 的警告;,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12096851/

相关文章:

c++ - 在 MSVC 中对 void * 执行指针运算时出错

c++ - 如何只关闭一个 GLUI 窗口

c++ - 如何在 FFMPEG 中填充/计算 motion_val?

C++ 列表迭代器到函数

c++ - __stdcall 类型定义结构

c++ - 重载 typedef 参数

c - 如何将大量变量发送到泛型函数?

c++ - Cocos2d 中 Sprite 的暂停/恢复 Action /动画

objective-c - 这是对typedef枚举的滥用还是误用(与具有静态内容的UITableView一起使用)

c - 跨平台使用 void 指针