public inbox for gcc-help@gcc.gnu.org
 help / color / mirror / Atom feed
* Problem with ambiguous overloaded operators
@ 2012-06-07 17:09 Arthur Schwarz
  2012-06-07 17:53 ` Jonathan Wakely
  0 siblings, 1 reply; 3+ messages in thread
From: Arthur Schwarz @ 2012-06-07 17:09 UTC (permalink / raw)
  To: gcc-help



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)

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: Problem with ambiguous overloaded operators
  2012-06-07 17:09 Problem with ambiguous overloaded operators Arthur Schwarz
@ 2012-06-07 17:53 ` Jonathan Wakely
  2012-06-07 17:59   ` Jonathan Wakely
  0 siblings, 1 reply; 3+ messages in thread
From: Jonathan Wakely @ 2012-06-07 17:53 UTC (permalink / raw)
  To: Arthur Schwarz; +Cc: gcc-help

On 7 June 2012 18:09, Arthur Schwarz wrote:
>
>
> 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.

Literals are not run-time values, they are compile-time values, a call
using a literal must be resolved at compile-time like any other call.

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

A long can be converted to any of the types you have overloaded the
function for, there is no "hierarchy" of types that a long would
prefer to convert to.

You should overload the function for all types you need it for or
explicitly convert the argument to one of the types you have
overloaded the function for.

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: Problem with ambiguous overloaded operators
  2012-06-07 17:53 ` Jonathan Wakely
@ 2012-06-07 17:59   ` Jonathan Wakely
  0 siblings, 0 replies; 3+ messages in thread
From: Jonathan Wakely @ 2012-06-07 17:59 UTC (permalink / raw)
  To: Arthur Schwarz; +Cc: gcc-help

On 7 June 2012 18:52, Jonathan Wakely wrote:
> On 7 June 2012 18:09, Arthur Schwarz wrote:
>>
>>
>> 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.
>
> Literals are not run-time values, they are compile-time values, a call
> using a literal must be resolved at compile-time like any other call.
>
>> 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

char can be promoted to int, and unsigned char can be promoted to unsigned int.

>> between unsigned (char  or long) and signed (char or long) since their ranges
>> are different.
>
> A long can be converted to any of the types you have overloaded the
> function for, there is no "hierarchy" of types that a long would
> prefer to convert to.

Actually that's not accurate, there is a conversion rank for integers,
but for the types you've used long doesn't "prefer" to convert to any
of them.

> You should overload the function for all types you need it for or
> explicitly convert the argument to one of the types you have
> overloaded the function for.

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2012-06-07 17:59 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2012-06-07 17:09 Problem with ambiguous overloaded operators Arthur Schwarz
2012-06-07 17:53 ` Jonathan Wakely
2012-06-07 17:59   ` Jonathan Wakely

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).