FIX: Error C2593: 'operator +' Is AmbiguousLast reviewed: September 18, 1997Article ID: Q128344 |
The information in this article applies to:
SYMPTOMSWhen enumerated types are used in some (+-*/|&^%) operator overloads, the Microsoft compilers listed above may generate the following message:
Error C2593: 'operator +' is ambiguousIn Visual C++, 32-bit edition, version 2.1, the following warnings are also displayed:
Warning C4387: 'enum Test __cdecl operator+(enum Test, enum Test)': was considered Warning C4388: and built-in global operator was consideredThe "Sample Code" section in this article demonstrates this problem.
CAUSEWithout determining that there is an exact operator overload match, the compiler is considering the enumerator types promoted to int type. This causes the C2593 ambiguous error.
RESOLUTIONUse one of the following three workarounds:
STATUSMicrosoft has confirmed this to be a bug in the Microsoft products listed at the beginning of this article. This bug was corrected in Visual C++ version 5.0.
MORE INFORMATION
Sample Code to Reproduce Problem
/* Compiler options needed: None. */ enum Test { ONE, TWO, THREE }; Test operator+(Test eTest, Test eTest2) { Test eRet = Test(int(eTest) + int(eTest2)); return eRet; } void main() { Test eTest=ONE, eTest2=TWO; eTest = eTest + eTest2; // C2593 error } Sample Code to Work Around Problem
/* Compiler options needed: None. */ enum Test { ONE=1, TWO, THREE }; template<class T> class CFix { T m_fix; public: CFix(T eFix) { m_fix = eFix; } operator T() { return m_fix; } }; Test operator+(CFix<Test> eTest, Test eTest2) { Test eRet = Test(int(eTest) + int(eTest2)); return eRet; } void main() { CFix<Test> eTest=ONE, eTest2=TWO; eTest = eTest + eTest2; } REFERENCESMore information about operator overloading can be found in "The Annotated C++ Reference Manual (Jan '94)" section 13.4.
|
Additional query words: cpp 9.00 8.00 8.00c 10.00 10.10 10.20
© 1998 Microsoft Corporation. All rights reserved. Terms of Use. |