2

I use enum class to set status returned by my classes but it seems that this can produce strange conflicts if a label of an enum is "OK" I guess. My project is a CMake project.

Example:

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

This code doesn't compile :

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

If I replace

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
};

by

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
};

It compiles...

Exception.hpp and ftdi.h (libftdi) have no enum and no #define OK.

didil
  • 693
  • 8
  • 22

0 Answers0