第一个枚举标签问题(枚举类)

时间:2015-07-06 12:15:42

标签: c++ enums ftdi

我使用enum class来设置我的类返回的状态,但是如果enum的标签是“OK”我认为这似乎会产生奇怪的冲突。我的项目是一个CMake项目。

示例:

FTDIDevice.hpp:

#ifndef __FTDIDEVICE_HPP
#define __FTDIDEVICE_HPP

#include <Exception.hpp>
#include <ftdi.h>
#include <string>
#include <vector>
#include <mutex>

typedef std::vector<uint8_t> FTDIData;

enum class FTDIDeviceStatus
{
    OK,
    CREATE_ERROR,
    INIT_ERROR,
    OPEN_ERROR,
    BAUDRATE_ERROR,
    SEND_ERROR,
    RECEIVE_ERROR,
    CLOSE_ERROR,
    NO_DATA,
    NOT_CONNECTED,
    FILE_NOT_FOUND
};

class FTDIDeviceException : public Exception
{
    private:

        static std::string statusStrings[];
        ftdi_context *context;

    public:

        FTDIDeviceException(FTDIDeviceStatus error, ftdi_context *context);
        virtual const char *what() const noexcept;
        virtual std::string who() const noexcept;
};

class FTDIDevice
{
    private:

        ftdi_context *context = nullptr;
        int16_t vendorId;                   
        int16_t productId;                  
        int32_t baudRate;                   
        bool connected;                     
        std::mutex mutex;                   

    public:

        static const std::string CLASS_NAME;

        FTDIDevice(const std::string &configFile) throw(FTDIDeviceException);
        ~FTDIDevice();
        FTDIDeviceStatus connect() throw(FTDIDeviceException);
        FTDIDeviceStatus disconnect() throw(FTDIDeviceException);
        FTDIDeviceStatus send(FTDIData &data) throw(FTDIDeviceException);
        FTDIDeviceStatus receive(FTDIData &data) throw(FTDIDeviceException);
        bool isConnected() const;
};

#endif

此代码无法编译:

core/io/FTDIDevice.hpp:32:5: error: expected identifier before '(' token
core/io/FTDIDevice.hpp:32:5: error: expected '}' before '(' token
core/io/FTDIDevice.hpp:32:5: error: expected unqualified-id before numeric constant
core/io/FTDIDevice.hpp:32:5: error: expected ')' before numeric constant
core/io/FTDIDevice.hpp:43:1: error: expected declaration before '}' token

如果我更换

enum class FTDIDeviceStatus
{
    OK,
    CREATE_ERROR,
    INIT_ERROR,
    OPEN_ERROR,
    BAUDRATE_ERROR,
    SEND_ERROR,
    RECEIVE_ERROR,
    CLOSE_ERROR,
    NO_DATA,
    NOT_CONNECTED,
    FILE_NOT_FOUND
};

通过

enum class FTDIDeviceStatus
{
    FOO,
    CREATE_ERROR,
    INIT_ERROR,
    OPEN_ERROR,
    BAUDRATE_ERROR,
    SEND_ERROR,
    RECEIVE_ERROR,
    CLOSE_ERROR,
    NO_DATA,
    NOT_CONNECTED,
    FILE_NOT_FOUND
};

它编译......

Exception.hpp和ftdi.h(libftdi)没有枚举,也没有#define OK。

0 个答案:

没有答案