|
-
July 18th, 2008, 08:59 AM
#1
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....
-
July 18th, 2008, 09:08 AM
#2
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?
-
July 18th, 2008, 09:15 AM
#3
Re: Overloading '=', getting errors
 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};
-
July 18th, 2008, 09:20 AM
#4
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;
-
July 21st, 2008, 02:08 AM
#5
Re: Overloading '=', getting errors
 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
-
July 30th, 2008, 06:52 AM
#6
Re: Overloading '=', getting errors
elements of an enum can only be assigned values at the point of declaration.
-
July 30th, 2008, 10:25 AM
#7
Re: Overloading '=', getting errors
 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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|