Operator Overloading Troubles
-
I'm new to operator overloading so this may come off as a stupid question, but I'll risk it. I'm making a simple BlackJack game and have two
ENUM
s that I want operators to work on. Here's a little of what I'm working with:enum SUIT { CLUB, DIAMOND, SPADE, HEART };
void operator++( SUIT &suit )
{
switch( suit )
{
case( CLUB ):
suit = DIAMOND;
break;
case( DIAMOND ):
suit = SPADE;
break;
case( SPADE ):
suit = HEART;
break;
case( HEART ):
suit = CLUB;
break;
default:
;//SHOULD NEVER GET HERE!!!
}
}The '--' operator looks nearly identical. I keep getting a compile error C2676: binary '++' : 'SUIT' does not define this operator or a conversion to a type acceptable to the predefined operator. Where's my problem coming from? Also, any ideas for tricks to overload the '+' & '-' operators without having to make a very long
switch
? Always Fear the Man with Nothing to Lose Jeryth -
I'm new to operator overloading so this may come off as a stupid question, but I'll risk it. I'm making a simple BlackJack game and have two
ENUM
s that I want operators to work on. Here's a little of what I'm working with:enum SUIT { CLUB, DIAMOND, SPADE, HEART };
void operator++( SUIT &suit )
{
switch( suit )
{
case( CLUB ):
suit = DIAMOND;
break;
case( DIAMOND ):
suit = SPADE;
break;
case( SPADE ):
suit = HEART;
break;
case( HEART ):
suit = CLUB;
break;
default:
;//SHOULD NEVER GET HERE!!!
}
}The '--' operator looks nearly identical. I keep getting a compile error C2676: binary '++' : 'SUIT' does not define this operator or a conversion to a type acceptable to the predefined operator. Where's my problem coming from? Also, any ideas for tricks to overload the '+' & '-' operators without having to make a very long
switch
? Always Fear the Man with Nothing to Lose JerythYour code seems to work fine in VS.NET 2K3:
SUIT f = CLUB;
++f;Are you trying to use the postfix version of
operator++
? If so, you need to add another function with a dummy int parameter:SUIT operator++(SUIT &suit,int){
SUIT old = suit;
++suit;
switch( old ) {
case( CLUB ):
return DIAMOND;
break;
case( DIAMOND ):
return SPADE;
break;
case( SPADE ):
return HEART;
break;
case( HEART ):
return CLUB;
break;
default:
return DIAMOND;
}
}- Mike
-
Your code seems to work fine in VS.NET 2K3:
SUIT f = CLUB;
++f;Are you trying to use the postfix version of
operator++
? If so, you need to add another function with a dummy int parameter:SUIT operator++(SUIT &suit,int){
SUIT old = suit;
++suit;
switch( old ) {
case( CLUB ):
return DIAMOND;
break;
case( DIAMOND ):
return SPADE;
break;
case( SPADE ):
return HEART;
break;
case( HEART ):
return CLUB;
break;
default:
return DIAMOND;
}
}- Mike
No it was prefix and I'm on 2K3 as well. However your code had a return on it. I understand the purpose of the return here, postfix == fetch value THEN increment, does the prefix need one too? Always Fear the Man with Nothing to Lose http://www.dragonfiresoft.com Jeryth
-
No it was prefix and I'm on 2K3 as well. However your code had a return on it. I understand the purpose of the return here, postfix == fetch value THEN increment, does the prefix need one too? Always Fear the Man with Nothing to Lose http://www.dragonfiresoft.com Jeryth
You can choose to have a return or not on the prefix, but to maintain the standard semantics of prefix increment it really should return a value. So, the declaration for the function changes to:
const SUIT &operator++(SUIT &suit)
and just add
return suit;
to the end of the function. For reference, here's the test program I'm using:
enum SUIT { CLUB, DIAMOND, SPADE, HEART };
const SUIT &operator++(SUIT &suit) {
switch( suit ) {
case( CLUB ):
suit = DIAMOND;
break;
case( DIAMOND ):
suit = SPADE;
break;
case( SPADE ):
suit = HEART;
break;
case( HEART ):
suit = CLUB;
break;
default:
;
}
return suit;
}
SUIT operator++(SUIT &suit,int){
SUIT old = suit;
++suit;
return old;
}
int _tmain(int argc, _TCHAR* argv[]) {SUIT f = CLUB; SUIT v = ++f; return 0;
}
Note the fixed postfix increment... I blame insufficient caffeine when I replied earlier :) - Mike
-
I'm new to operator overloading so this may come off as a stupid question, but I'll risk it. I'm making a simple BlackJack game and have two
ENUM
s that I want operators to work on. Here's a little of what I'm working with:enum SUIT { CLUB, DIAMOND, SPADE, HEART };
void operator++( SUIT &suit )
{
switch( suit )
{
case( CLUB ):
suit = DIAMOND;
break;
case( DIAMOND ):
suit = SPADE;
break;
case( SPADE ):
suit = HEART;
break;
case( HEART ):
suit = CLUB;
break;
default:
;//SHOULD NEVER GET HERE!!!
}
}The '--' operator looks nearly identical. I keep getting a compile error C2676: binary '++' : 'SUIT' does not define this operator or a conversion to a type acceptable to the predefined operator. Where's my problem coming from? Also, any ideas for tricks to overload the '+' & '-' operators without having to make a very long
switch
? Always Fear the Man with Nothing to Lose Jeryth -
Hi Jeryth You cannot redefine the meaning of an operator when applied to built-in data types. Your SUIT enum is nothing but an ordinary int. I think that this is your problem... greets, Jason
That's what I thought as well,
enum
s get referenced to asint
s for boolean and conditional arguments. I'm not sure but I think .NET changed that. When I hadn't overloaded any operator I still got an error saying no operator existed that returned an appropriate type. Always Fear the Man with Nothing to Lose DragonFire Software Jeryth