2017-01-25 2 views
-3

In einer Klasse habe ich die folgende enum ad Operatorüberladungen, wie in How to use enums as flags in C++? gezeigt.C++ Überlastungsfehler des Bedieners

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

der Compiler kehren die followinf Fehler:

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

Warum ist die Überlastung nicht korrekt?

Weitere Informationen, nach ein paar Kommentaren/Antwort: Zuerst, Danke für die Kommentare und Antworten.

Der Operator ist Mitglied einer Klasse, in der die Enumeration definiert ist. Ich bin neu in C++ und oop .... kann jemand ein Beispiel für Code für einen Operator meber einer Klasse vorschlagen?

Hier ist der vollständige Klassencode, ich versuche, Operator + und | zu setzen zur Verwendung der einzelnen Werte der Enums als Bitmaske für die Parameter void initialize(OperationMode MDR0_OperationMode, AddOperationMode MDR1_AddOperationMode) Funktion.

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

Ist 'Betreiber |' Mitglied einer Klasse? Wenn ja, dann braucht es nur ein Argument, d. H. Seine rechte Seite. – vsoftco

+0

Arbeiten Sie wie erwartet [hier] (http://coliru.stacked-crooked.com/a/4f2cedffa7c1e88d). – Jarod42

+0

[Kann nicht reproduziert werden] (http://ideone.com/2dTcbJ). –

Antwort

0

Von der Fehlermeldung

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

MngSpiLS7633R ist entweder ein Namespace oder ein Klassenname, den operator| ist Mitglied. Wenn dies der Fall ist, darf der Operator nur ein Argument haben oder als freie Funktion definiert sein (außerhalb der Klasse MngSpiLS7633R).

0

Ich habe das Problem mit friend Modifizierer für die Operator-Deklaration innerhalb der Klasse gelöst.

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

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

Dann werden die Betreiber außerhalb der Klasse definiert:

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)); 
} 
Verwandte Themen