Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Operator Overloading Troubles

Operator Overloading Troubles

Scheduled Pinned Locked Moved C / C++ / MFC
helpquestiongame-dev
6 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • J Offline
    J Offline
    Jeryth
    wrote on last edited by
    #1

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

    Z J 2 Replies Last reply
    0
    • J 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 ENUMs 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

      Z Offline
      Z Offline
      ZoogieZork
      wrote on last edited by
      #2

      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

      J 1 Reply Last reply
      0
      • Z ZoogieZork

        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

        J Offline
        J Offline
        Jeryth
        wrote on last edited by
        #3

        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

        Z 1 Reply Last reply
        0
        • J 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

          Z Offline
          Z Offline
          ZoogieZork
          wrote on last edited by
          #4

          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

          1 Reply Last reply
          0
          • J 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 ENUMs 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

            J Offline
            J Offline
            jason99
            wrote on last edited by
            #5

            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

            J 1 Reply Last reply
            0
            • J jason99

              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

              J Offline
              J Offline
              Jeryth
              wrote on last edited by
              #6

              That's what I thought as well, enums get referenced to as ints 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

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups