Okay here's what i got. -
class A
-
{
-
. . .
-
private:
-
enum Type
-
{
-
A, B, C,
-
};
-
-
enum State
-
{
-
Z, Y, X,
-
};
-
-
class SubA
-
{
-
. . .
-
private:
-
Type MyType;
-
State MyState;
-
}
-
}
-
//in a function i do this
-
int randomnum = rand()%3;
-
SubAObject.MyType = (Type)randomnum; //this works fine
-
-
randomnum = rand()%3; //different random number, in actual code it is of differnt size
-
SubAObject.MyState = (State)randomnum; //error
-
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.
6 4101
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.
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.
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.
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. -
class System()
-
{
-
System();
-
~System();
-
void AddItem();
-
-
private:
-
enum ItemType
-
{
-
Type1 = 0,
-
Type2,
-
Type3,
-
Type4,
-
Type5,
-
Type6,
-
Type7,
-
Type8,
-
Type9,
-
Type10,
-
Type11,
-
Type12,
-
Type13,
-
Type14,
-
Type15,
-
};
-
-
enum ItemMode
-
{
-
Mode1 = 0,
-
Mode2,
-
Mode3,
-
};
-
-
class ItemStruct
-
{
-
public:
-
ItemStruct();
-
-
ItemMode Mode;
-
ItemType Type;
-
};
-
};
-
-
void System::AddItem()
-
{
-
-
ItemStruct TempItem;
-
-
int randtemp = rand()%15;
-
TempItem.Type = (ItemType)randtemp;
-
-
randtemp = rand()%3;
-
TempItem.Mode = (ItemMode)randtemp;
-
//////////////////////////////////////////////////////////
-
////////I can make this work by doing this instead//
-
/* if(randtemp == 0)
-
TempItem.Mode = ItemMode::Mode1;
-
else
-
if(randtemp == 1)
-
TempItem.Mode = ItemMode::Mode2;
-
ect.*/
-
}
-
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. -
enum Stuff { A, B};
-
-
Stuff var = (Stuff) 20;
-
cout << var << endl; //you see 20
-
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.
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.
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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...
|
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
{
|
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
|
by: Harris |
last post by:
Dear all,
I have the following codes:
======
public enum Enum_Value
{
Value0 = 0,
Value1 = 10,
|
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){}
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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)...
|
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...
|
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....
|
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
|
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...
| |