1

I'm having some problems trying to overload the += operator for an enum I've defined within a namespace. I shouldn't need to actually use the operator, however, a library I'm using (boost::icl) requires that the += operator is defined for the data I'm storing within the interval map. Whenever I try to compile the code below, I get the following compiler error using Intel C++:

error : enum "test::events" has no member "operator+="

Any suggestions?

test.h:

namespace test {

    enum events {
        SHUTIN = 0,
        ACTIVE,
        RECOMPLETE,
        CTI,
        RTP
    };

   events & events::operator+= (const events &rhs);

}; // end namespace test

test.cpp:

test::events & test::events::operator+= (const test::events &rhs) {
    return *this;
}
DickNixon
  • 151
  • 2
  • 7
  • You can't overload `operator+=` for enums. – chris Sep 09 '13 at 16:11
  • That's what I was thinking, but I couldn't find anything online that explicitly said so... Thanks for confirming. – DickNixon Sep 09 '13 at 16:13
  • Even if you could, you cannot invent member functions for types that don't already have them. – juanchopanza Sep 09 '13 at 16:15
  • @juanchopanza Really? It would follow (at least to me) that if you could define operators for user-defined data structures, that you should be able to overload or create new operators for the existing types. TIL. Thanks for the info. – DickNixon Sep 09 '13 at 16:25
  • You would have to declare those member operators inside the definition of the user defined type. – juanchopanza Sep 09 '13 at 16:26

1 Answers1

1

You can use a free function:

events & operator+= (events &lhs, const events &rhs);

(tested with GCC 4.8, if Intel C++ rejects it I'd think it's a bug)

Daniel Frey
  • 55,810
  • 13
  • 122
  • 180