This is the mail archive of the
gcc-help@gcc.gnu.org
mailing list for the GCC project.
Problem with ambiguous overloaded operators
- From: Arthur Schwarz <aschwarz1309 at att dot net>
- To: gcc-help at gcc dot gnu dot org
- Date: Thu, 7 Jun 2012 10:09:13 -0700 (PDT)
- Subject: Problem with ambiguous overloaded operators
I am using g++ 4.5.3 as my compiler and NetBeans 7.1.2 as my IDE.
What I wnat to do is to overload operators using all selected types. What I get
are error messages indicating that the usage is ambiquous. I understaad that
the runtime usage may be ambiguous using literal values. I don't understand why
a typed variable would cause ambiguity. And I would like to know how to code
around the issue. The issue (to me) is that char can be widened to long, and
unsigned char can be widened to unsigned long, but how do I take care of issues
between unsigned (char or long) and signed (char or long) since their ranges
are different.
Examplar code is:
#include <iostream>
using namespace std;
#ifdef BOOL
# undef BOOL
#endif
#ifdef CHAR
# undef CHAR
#endif
#ifdef UCHAR
# undef UCHAR
#endif
#ifdef LONG
# undef LONG
#endif
#ifdef ULONG
# undef ULONG
#endif
//---------------------------------------------------------------------
// Typedef
//---------------------------------------------------------------------
typedef bool BOOL;
typedef unsigned char UCHAR; // 8-bits
typedef signed char CHAR; // 8-bits
typedef unsigned long ULONG; // 32-bits
typedef signed long LONG; // 32-bits
class datumClass {
union Data { // data contents
BOOL Bool; // compiler defined
CHAR Chr; // 8-bits
UCHAR UChr; // 8-bits
LONG Long; // 32-bits
ULONG ULong; // 32-bits
}; // union Data
Data datum;
}; // datumClass
class opClass {
private:
public:
virtual datumClass& bxorAsgn (datumClass& Y, BOOL X) = 0;// operator^=
virtual datumClass& bxorAsgn (datumClass& Y, UCHAR X) = 0;// operator^=
virtual datumClass& bxorAsgn (datumClass& Y, CHAR X) = 0;// operator^=
virtual datumClass& bxorAsgn (datumClass& Y, ULONG X) = 0;// operator^=
// virtual datumClass& bxorAsgn (datumClass& Y, LONG X) = 0;// operator^=
}; // class opClass
class baseClass : public datumClass {
opClass* operation;a
public:
opClass* getOperation() { return operation; }
}; // baseClass
class inheritClass : public baseClass {
public:
baseClass& operator^=(LONG X) { return getOperation()->bxorAsgn(*this, X);
}// Y ^= V
}; // class inheritClass
int main(int argc, char** argv) {
}
The diagnostic messages are:
"/usr/bin/make" -f nbproject/Makefile-Debug.mk QMAKE= SUBPROJECTS= .build-conf
make[1]: Entering directory `/c/home/skidmarks/Projects/Test/Test'
"/usr/bin/make" -f nbproject/Makefile-Debug.mk
dist/Debug/Cygwin_4.x-Windows/test.exe
make[2]: Entering directory `/c/home/skidmarks/Projects/Test/Test'
mkdir -p build/Debug/Cygwin_4.x-Windows
rm -f build/Debug/Cygwin_4.x-Windows/main.o.d
g++.exe -c -g -MMD -MP -MF build/Debug/Cygwin_4.x-Windows/main.o.d -o
build/Debug/Cygwin_4.x-Windows/main.o main.cpp
main.cpp: In member function âbaseClass& inheritClass::operator^=(LONG)â:
main.cpp:60:78: error: call of overloaded âbxorAsgn(inheritClass&, LONG&)â is
ambiguous
nbproject/Makefile-Debug.mk:65: recipe for target
`build/Debug/Cygwin_4.x-Windows/main.o' failed
main.cpp:45:23: note: candidates are: virtual datumClass&
opClass::bxorAsgn(datumClass&, BOOL)
make[2]: Leaving directory `/c/home/skidmarks/Projects/Test/Test'
main.cpp:46:23: note: virtual datumClass&
opClass::bxorAsgn(datumClass&, UCHAR)
nbproject/Makefile-Debug.mk:58: recipe for target `.build-conf' failed
main.cpp:47:23: note: virtual datumClass&
opClass::bxorAsgn(datumClass&, CHAR)
make[1]: Leaving directory `/c/home/skidmarks/Projects/Test/Test'
main.cpp:48:23: note: virtual datumClass&
opClass::bxorAsgn(datumClass&, ULONG)
nbproject/Makefile-impl.mk:39: recipe for target `.build-impl' failed
make[2]: *** [build/Debug/Cygwin_4.x-Windows/main.o] Error 1
make[1]: *** [.build-conf] Error 2
make: *** [.build-impl] Error 2
BUILD FAILED (exit value 2, total time: 555ms)