CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7
  1. #1
    Join Date
    Jan 2008
    Posts
    41

    Overloading '=', getting errors

    Anything wrong with this overloading
    Code:
    TankStatus operator=(int number, TankStatus ts)
    {
    	return ts = number;
    }
    
    //: error C2801: 'operator =' must be a non-static member
    TankStatus is enum type....

  2. #2
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,637

    Re: Overloading '=', getting errors

    You can only overload operators on classes, structs and unions. MSDN explains that if you look up the error number. Why would you want to overload the = operator on an enum anyway?

  3. #3
    Join Date
    Jan 2008
    Posts
    41

    Re: Overloading '=', getting errors

    Quote Originally Posted by GCDEF
    You can only overload operators on classes, structs and unions. MSDN explains that if you look up the error number. Why would you want to overload the = operator on an enum anyway?
    because...
    Code:
    if ( (ts>3 ) || (ts<0) ) {
    	ts=0 ;
    }
    
    // : error C2440: '=' : cannot convert from 'int' to TankStatus'
    again, ts is enum.... // enum TankStatus {unknown, empty, half, full};

  4. #4
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,637

    Re: Overloading '=', getting errors

    The point of an enum is to provide names for its possible values. You should use those names when assigning it a value, otherwise there's no point in using an enum.
    Code:
    enum TankStatus
    {
       unknown,
       empty,
       full
    }:
    
    TanksStatus ts;
    
    ts = unknown;

  5. #5
    Join Date
    Jan 2008
    Posts
    41

    Re: Overloading '=', getting errors

    Quote Originally Posted by GCDEF
    The point of an enum is to provide names for its possible values. You should use those names when assigning it a value, otherwise there's no point in using an enum.
    Code:
    enum TankStatus
    {
       unknown,
       empty,
       full
    }:
    
    TanksStatus ts;
    
    ts = unknown;
    hey you right, thanx

  6. #6
    Join Date
    May 2008
    Posts
    32

    Re: Overloading '=', getting errors

    elements of an enum can only be assigned values at the point of declaration.

  7. #7
    Join Date
    Aug 2005
    Location
    San Diego, CA
    Posts
    1,054

    Re: Overloading '=', getting errors

    Quote Originally Posted by s_kannan55
    elements of an enum can only be assigned values at the point of declaration.
    If you define a variable that happens to be an enum (and it is not a const) you can assign to it as much as you want.
    Code:
    enum colors
    {
       RED,
       BLUE,
       GREEN
    }
    
    colors textColor (RED);
    
    // later I can do this
    textColor = BLUE;
    Am I misunderstanding your point?

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured