FMA
FMA

Reputation: 33

Errors using enum type in C++ code

I just implemented this class :

class MIXIM_API DSRCmsgID : public cObject
{
public:
    /** @brief Constant with all values set to 0. */
    static const DSRCmsgID ZERO;

public:

    //int msgID;
     enum msgID {
        DSRCmsgID_reserved  = 0,
        DSRCmsgID_alaCarteMessage   = 1,
        DSRCmsgID_DSRCmsgID    = 2,
        DSRCmsgID_DSRCmsgIDVerbose = 3,
        DSRCmsgID_commonSafetyRequest   = 4,
        DSRCmsgID_emergencyVehicleAlert = 5,
        DSRCmsgID_intersectionCollisionAlert    = 6,
        DSRCmsgID_mapData   = 7,
        DSRCmsgID_nmeaCorrections   = 8,
        DSRCmsgID_probeDataManagement   = 9,
        DSRCmsgID_probeVehicleData  = 10,
        DSRCmsgID_roadSideAlert = 11,
        DSRCmsgID_rtcmCorrections   = 12,
        DSRCmsgID_signalPhaseAndTimingMessage   = 13,
        DSRCmsgID_signalRequestMessage  = 14,
        DSRCmsgID_signalStatusMessage   = 15,
        DSRCmsgID_travelerInformation   = 16
    };

private:
  void copy(const DSRCmsgID& other) {msgID = other.msgID; }

public:
    /** @brief Default constructor. */
    DSRCmsgID()
        : msgID() {}

    /** @brief Initializes a DSRCmsgIDinate. */
    DSRCmsgID(enum msgID )
           : msgID(msgID) {}

    /** @brief Initializes DSRCmsgIDinate from other DSRCmsgIDinate. */
    DSRCmsgID(const DSRCmsgID& other)
        : cObject(other) { copy(other); }

    /** @brief Returns a string with the value of the DSRCmsgIDinate. */
    std::string info() const;

};


inline std::ostream& operator<<(std::ostream& os, const DSRCmsgID& DSRCmsgID)
{
    return os << "(" << DSRCmsgID.msgID << ")";
}

inline std::string DSRCmsgID::info() const {
    std::stringstream os;
    os << *this;
    return os.str();
}

I got these errors:

Upvotes: 0

Views: 1662

Answers (1)

Paul R
Paul R

Reputation: 213039

msgID is currently declared as a type, not a variable - you could just change:

enum msgID {
    DSRCmsgID_reserved  = 0,
    // ...
    DSRCmsgID_travelerInformation   = 16
};

to:

enum {
    DSRCmsgID_reserved  = 0,
    // ...
    DSRCmsgID_travelerInformation   = 16
} msgID;

however it would probably be more convenient to declare a type:

enum MsgID {  // the type of this enum is `MsgID`
    DSRCmsgID_reserved  = 0,
    // ...
    DSRCmsgID_travelerInformation   = 16
} msgID;      // and we also have an instance variable of this type, `msgID`

then you can also fix this constructor:

DSRCmsgID(enum msgID )
       : msgID(msgID) {}

which would become:

DSRCmsgID(MsgID msgID )
       : msgID(msgID) {}

Upvotes: 8

Related Questions