473,320 Members | 2,158 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Weird int to enum casting error.

464 Expert 256MB
Okay here's what i got.

Expand|Select|Wrap|Line Numbers
  1. class A
  2. {
  3.   . . .
  4.   private:
  5.   enum Type
  6.   {
  7.     A, B, C,  
  8.  };  
  9.  
  10.  enum State
  11. {
  12.    Z, Y, X,
  13. };
  14.  
  15. class SubA
  16. {
  17.     . . .
  18.     private: 
  19.     Type  MyType;
  20.     State MyState;
  21. }
  22. }
  23. //in a function i do this
  24. int randomnum = rand()%3;
  25. SubAObject.MyType = (Type)randomnum; //this works fine
  26.  
  27. randomnum = rand()%3; //different random number, in actual code it is of differnt size
  28. SubAObject.MyState = (State)randomnum; //error
  29.  
the error i receive is
error C2440: '=' : cannot convert from 'CString' to 'ClassA::State'
No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called.

Why can it do it for one enum, but not for the other. They are defined the same way, the semi-colllins are where they are suppose to be any ideas? I'm using Visual Studios 2003, in a MFC application.
Nov 15 '07 #1
6 4101
weaknessforcats
9,208 Expert Mod 8TB
There are problems here that go beyond what youbare reporting.

Like, your class name A duplicates a value in the enum Type.
Like, your class declarations do not have terminating semi-colons.
Like, you are reporting an error about a CString when there are no CString variables in your code.

I got a ton of errors using Visual Studio.NET 2005 but not the one you say you are getting.

Fix this code and repost when it's in a state that when I compile it using Visual Studio.NET 2005 that I get the same errors you do.
Nov 16 '07 #2
Banfa
9,065 Expert Mod 8TB
error C2440: '=' : cannot convert from 'CString' to 'ClassA::State'
Read the error, you are not trying to cast int to enum which is possible you are trying to cast CString to enum which is not possible.
Nov 16 '07 #3
weaknessforcats
9,208 Expert Mod 8TB
you are trying to cast CString to enum which is not possible.
Yeah. I saw that,. But where is the CString?? All the OP casts are from int to enum.
Nov 16 '07 #4
Studlyami
464 Expert 256MB
Sorry about that i should of payed closer attention to the example. Here is the code. I can't find any reason why it detects a CString. I stripped out the items and put it in a console app and it compiles so I'm guessing something else is happening any idea? The app is way to large to post, but i can put up some of the other items too if needed. I don't see a CString anywhere.

Expand|Select|Wrap|Line Numbers
  1. class System()
  2. {
  3.       System();
  4.       ~System();
  5.       void AddItem();
  6.  
  7. private:
  8.     enum ItemType
  9.     {
  10.         Type1 = 0,
  11.         Type2,
  12.         Type3,
  13.         Type4,
  14.         Type5,
  15.         Type6,
  16.         Type7,
  17.         Type8, 
  18.         Type9,
  19.         Type10,
  20.         Type11, 
  21.         Type12,
  22.         Type13, 
  23.         Type14,
  24.         Type15,
  25.     };
  26.  
  27.     enum ItemMode
  28.     {
  29.         Mode1 = 0,
  30.         Mode2,
  31.         Mode3,
  32.     };
  33.  
  34.  class ItemStruct
  35.  {
  36.     public:
  37.         ItemStruct();
  38.  
  39.         ItemMode Mode;
  40.         ItemType Type;
  41.     };
  42. };
  43.  
  44. void System::AddItem()
  45. {
  46.  
  47.  ItemStruct TempItem;
  48.  
  49. int randtemp = rand()%15;
  50. TempItem.Type = (ItemType)randtemp;
  51.  
  52.     randtemp = rand()%3;
  53. TempItem.Mode = (ItemMode)randtemp;
  54. //////////////////////////////////////////////////////////
  55. ////////I can make this work by doing this instead//
  56. /*  if(randtemp == 0)
  57.         TempItem.Mode = ItemMode::Mode1;
  58.    else
  59.    if(randtemp == 1)
  60.         TempItem.Mode = ItemMode::Mode2;
  61.  ect.*/
  62. }
  63.  
Nov 16 '07 #5
weaknessforcats
9,208 Expert Mod 8TB
If it compiles as a console app, then it has something to do with using MFC.

However it goes, an enum is just a list of named integer values. When you typecast to an enum, the compiler is told: "I don't care that the value in the int is crap. I say it's a valid enum value. Now do what I tell ya."

The end result is an enum with a value that's not in the enum list.
Expand|Select|Wrap|Line Numbers
  1. enum Stuff { A, B};
  2.  
  3. Stuff var = (Stuff) 20;
  4. cout << var << endl;   //you see 20
  5.  
Please don't do that.

Also:
error C2440: '=' : cannot convert from 'CString' to 'ClassA::State'
This is not the complete error. You omitted the file name and the line number. So I can't tell if the error comes from the code you posted.
Nov 16 '07 #6
Studlyami
464 Expert 256MB
the error came from the file and was on the specified line, but i did find the problem and i am ashamed to admit what it was. There was a static CString declared with the same name so on the cast it took the CString variable vs the enum. *Sigh* sorry guys, but thanks for the input.
Nov 16 '07 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

20
by: Glenn Venzke | last post by:
I'm writing a class with a method that will accept 1 of 3 items listed in an enum. Is it possible to pass the item name without the enum name in your calling statement? EXAMPLE: public enum...
6
by: James Brown | last post by:
Hi, I have the following enum declared: enum TOKEN { TOK_ID = 1000, TOK_NUMBER, TOK_STRING, /*lots more here*/ }; What I am trying to do is _also_ represent ASCII values 0-127 as TOKENs...
21
by: Andreas Huber | last post by:
Hi there Spending half an hour searching through the archive I haven't found a rationale for the following behavior. using System; // note the missing Flags attribute enum Color {
18
by: Visual Systems AB \(Martin Arvidsson\) | last post by:
Hi! I have created an enum list like this: enum myEnum : int { This = 2, That, NewVal = 10, LastItm
7
by: Harris | last post by:
Dear all, I have the following codes: ====== public enum Enum_Value { Value0 = 0, Value1 = 10,
2
by: NullQwerty | last post by:
Hey folks, So, I've got three enum types: enum enum1 enum enum2 enum enum3 And then I've got a function overloaded three times to accept each enum type: private void func(enum1 myenum){}
2
by: JB | last post by:
Hi All, I'm pulling my hair over this and could really do with a bit of help. I'm using various different enums as bit fields and I'd like to create a set of generic Functions or Subs to...
11
by: =?Utf-8?B?dG9iaXdhbl9rZW5vYmk=?= | last post by:
The following code is in a custom deserializer: object value = (int) 1; string nameToParse = Enum.GetName(field.FieldType, value); value = Enum.Parse(field.FieldType, nameToParse); Currently...
4
by: harsh.murari | last post by:
In the example below, a single object (Network) is implementing 2 interfaces (INetworkA and INetworkB). Both of these interfaces derive from the IBase interface (similar to the IUnknown interface...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.