C ++运算符重载问题

时间:2017-01-25 15:25:26

标签: c++ enums arduino operator-overloading

在课程中,我有以下枚举广告运算符重载,如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 ) );
}

编译器返回followinf错误:

../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 )

为什么过载不正确?

其他信息,经过一些评论/答案: 首先,感谢您的评论和回答。

运算符是类的成员,其中定义了枚举。 我是c ++和oop的新手....有人可以建议一个类的操作符的代码示例吗?

这里有完整的类代码,我试图设置operator +和|使用枚举的单个值作为void initialize(OperationMode MDR0_OperationMode, AddOperationMode MDR1_AddOperationMode)函数的参数的位掩码。

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;

};

2 个答案:

答案 0 :(得分:0)

来自错误消息

error: 'MngSpiLS7633R::OperationMode MngSpiLS7633R::operator|...

MngSpiLS7633Roperator|所属的命名空间或类名。如果是这种情况,则操作符必须只接受一个参数或定义为自由函数(在类MngSpiLS7633R之外)。

答案 1 :(得分:0)

我已经使用friend修饰符为类中的操作符声明解决了问题。

friend AddOperationMode operator | ( AddOperationMode lhs, AddOperationMode rhs );

friend AddOperationMode operator + ( AddOperationMode lhs, AddOperationMode rhs );

然后在类外定义运算符:

MngSpiLS7366R::OperationMode operator | (MngSpiLS7366R::OperationMode lhs, MngSpiLS7366R::OperationMode rhs )
{
// Cast to int first otherwise we'll just end up recursing
return static_cast< MngSpiLS7366R::OperationMode >( static_cast< int >( lhs ) | static_cast< int >( rhs ) );
}

MngSpiLS7366R::OperationMode operator + (MngSpiLS7366R::OperationMode lhs, MngSpiLS7366R::OperationMode rhs )
{
// Cast to int first otherwise we'll just end up recursing
return static_cast< MngSpiLS7366R::OperationMode >( static_cast< int >( lhs ) | static_cast< int >( rhs ) );
}