【问题标题】:Wrapper for Phidgets not workingPhidg​​ets 的包装器不工作
【发布时间】:2015-01-20 19:11:52
【问题描述】:

我为phidgets C 库制作了一个 C++ 包装器,我目前有一个用于通用 Phidg​​et 设备的PhidgetDevice.h,这是标题:

#ifndef PHIDGET_DEVICE_H
#define PHIDGET_DEVICE_H

#include <phidget21.h>
#include <vector>
#include <iostream>
#include <string>


#define COMMON_IO_EVENT_CALLBACK(name) int CCONV name(CPhidgetHandle phid, void* userPtr)
#define ERROR_EVENT_CALLBACK(name) int CCONV name(CPhidgetHandle phid, void *userPtr, int errorCode, const char *errorString)

enum DeviceType
{
    DEVICE_NULL,
    DEVICE_KIT, 
    DEVICE_TEMPERATURE_SENSOR, 
    DEVICE_MOTION_SENSOR
};


class PhidgetDevice /* base class for every phidget device*/
{
public:
    PhidgetDevice() : m_type(DEVICE_NULL) {}
    explicit PhidgetDevice(DeviceType type) : m_type(type) {}
    void SetType(DeviceType type) { m_type = type; }

    virtual DeviceType GetType() { return m_type; }

    virtual void CCONV SetAttachHandler(CPhidgetHandle IFK, int(__stdcall * callback) (CPhidgetHandle phid, void *userPtr), void *userptr) { CPhidget_set_OnAttach_Handler(IFK, callback, userptr); }
    virtual void CCONV SetDetachHandler(CPhidgetHandle handle, int(__stdcall * callback) (CPhidgetHandle phid, void* userPtr), void * userptr) { CPhidget_set_OnDetach_Handler(handle, callback, userptr); }
    virtual void CCONV SetErrorHandler(CPhidgetHandle handle, int(__stdcall * callback) (CPhidgetHandle phid, void* userPtr, int errorCode, const char *errorString), void * userptr) { CPhidget_set_OnError_Handler(handle, callback, userptr); }
protected:
    DeviceType m_type;
};
#endif

这使得凌乱的 C Phidg​​et 函数看起来好多了。 现在,这在 Visual Studio 2013 上编译得很好,但是当我尝试使用 g++ 4.8 编译它时,包括-std=c++11 我得到了:

Phidg​​etDevice.h:34:72: 错误:在‘*’令牌虚拟之前预期‘)’ 无效 CCONV SetAttachHandler(CPhidg​​etHandle IFK,int(__stdcall * 回调) (CPhidg​​etHandle phid, void *userPtr), void *userptr) { CPhidg​​et_set_OnAttach_Handler(IFK, 回调, userptr); }

还有更多,不过都在抱怨函数指针。

我的函数指针有什么问题?

【问题讨论】:

    标签: c++ c++11


    【解决方案1】:

    您的函数指针定义很好,是 __stdcall 关键字导致 gcc 出现问题。 __stdcall 关键字定义编译器将用于指定函数的调用约定;特别是__stdcall 本身(双下划线然后是 stdcall 措辞)是用于调用约定的 MS 特定关键字,如果您希望使用 gcc 维护它,您可以执行以下操作:

    #ifndef WIN32
        #ifndef __stdcall
            #define __stdcall __attribute__((stdcall))
        #endif
    #endif
    

    尽管您可能会发现它在很大程度上会被编译器忽略(作为 gcc 警告:warning: 'stdcall' attribute ignored)。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-12-06
      • 1970-01-01
      • 1970-01-01
      • 2018-05-19
      • 2021-05-29
      • 1970-01-01
      • 2011-12-02
      相关资源
      最近更新 更多