In a class I have the following enum ad Operator overloads, as show in How to use enums as flags in C++?.
enum OperationMode
{
//...
};
OperationMode operator | (OperationMode lhs, OperationMode rhs )
{
// Cast to int first otherwise we'll just end up recursing
return static_cast< OperationMode >( static_cast< int >( lhs ) | static_cast< int >( rhs ) );
}
OperationMode operator + (OperationMode lhs, OperationMode rhs )
{
// Cast to int first otherwise we'll just end up recursing
return static_cast< OperationMode >( static_cast< int >( lhs ) | static_cast< int >( rhs ) );
}
the compiler return the followinf errors:
../MngSpiLS7633R/MngSpiLS7633R.h:40:84: error: 'MngSpiLS7633R::OperationMode MngSpiLS7633R::operator|(MngSpiLS7633R::OperationMode, MngSpiLS7633R::OperationMode)' must take exactly one argument
inline OperationMode operator | (const OperationMode lhs, const OperationMode rhs )
^
../MngSpiLS7633R/MngSpiLS7633R.h:46:72: error: 'MngSpiLS7633R::OperationMode MngSpiLS7633R::operator+(MngSpiLS7633R::OperationMode, MngSpiLS7633R::OperationMode)' must take either zero or one argument
inline OperationMode operator + (OperationMode lhs, OperationMode rhs )
Why the overload isn't correct?
Additional Informations, after some comments/Answer: First, Thanks for the comments and answer.
The operator is member of a class, where the enum is defined. I'm new in c++ and oop.... can anyone suggest an example of code for an operator meber of a class?
Here there is the complete class code, im trying to set operator + and | for using the single values of the enums as bitmask for the parameters of void initialize(OperationMode MDR0_OperationMode, AddOperationMode MDR1_AddOperationMode)
function.
class MngSpiLS7633R
{
public:
enum OperationMode
{
Om_Quad_None = 0x00,
Om_Quad_x1 = 0x01,
Om_Quad_x2 = 0x02,
Om_Quad_x4 = 0x03,
Om_Count_freeRun = 0x00,
Om_Count_SingleCycle = 0x04,
Om_Count_RangeLimit = 0x08,
Om_Count_ModuloN = 0x0C,
Om_Index_disabled = 0x00,
Om_Index_loadCNTR = 0x10,
Om_Index_resetCNTR = 0x20,
Om_Index_loadOTR = 0x30,
Om_AsynIndex_async = 0x00,
Om_AsynIndex_sync = 0x40,
Om_ClockFilter_1x = 0x00,
Om_ClockFilter_2x = 0x80
};
OperationMode operator | (OperationMode lhs, OperationMode rhs )
{
// Cast to int first otherwise we'll just end up recursing
return static_cast< OperationMode >( static_cast< int >( lhs ) | static_cast< int >( rhs ) );
}
OperationMode operator + (OperationMode lhs, OperationMode rhs )
{
// Cast to int first otherwise we'll just end up recursing
return static_cast< OperationMode >( static_cast< int >( lhs ) | static_cast< int >( rhs ) );
}
enum AddOperationMode
{
Aom_CouterMode_4byte = 0x00,
Aom_CouterMode_3byte = 0x01,
Aom_CouterMode_2byte = 0x02,
Aom_CouterMode_1byte = 0x03,
Aom_Counting_Enabled = 0x00,
Aom_Counting_Disabled = 0x04,
//bit-3 not used = 0x08
Aom_b4flag_Nop = 0x00,
Aom_b4flag_onIDX = 0x10,
Aom_b5flag_Nop = 0x00,
Aom_b5flag_onCMP = 0x20,
Aom_b6flag_Nop = 0x00,
Aom_b6flag_onBW = 0x40,
b7flag_Nop = 0x00,
b7flag_onCY = 0x80,
};
AddOperationMode operator | ( AddOperationMode lhs, AddOperationMode rhs )
{
// Cast to unsigned char first otherwise we'll just end up recursing
return static_cast< AddOperationMode >( static_cast< unsigned char >( lhs ) | static_cast< unsigned char >( rhs ) );
}
AddOperationMode operator + ( AddOperationMode lhs, AddOperationMode rhs )
{
// Cast to unsigned char first otherwise we'll just end up recursing
return static_cast< AddOperationMode >( static_cast< unsigned char >( lhs ) | static_cast< unsigned char >( rhs ) );
}
MngSpiLS7633R(int SSPin);
MngSpiLS7633R(int SSPin, bool ExternalNot);
virtual ~MngSpiLS7633R();
void initialize(OperationMode MDR0_OperationMode, AddOperationMode MDR1_AddOperationMode);
void resetCounter();
long get();
void set(long initValue);
private:
static const char SpiBitOrder = MSBFIRST;
static const char SpiMode = SPI_MODE0;
static const long SpiClock = 4000000; //4 Mhz maximum ratings
int SSEnabledLevel;
int SSDisabledLevel;
int SSPin;
bool SSExternalNot;
};