Title is a little vague but here is my problem.
I have an enumeration like this:
main.h
[cpp]
typedef enum {
FOO,
BAR,
} Foo;
char* getF(Foo e);
[/cpp]
main.c
[cpp]
#include "main.h"
char* getF(Foo e)
{
char* data;
switch(e)
{
case FOO:
data = "foo";
break;
case BAR:
data = "bar";
break;
default:
//Should never happen
break;
}
return data;
}
[/cpp]
getFoo() takes a enum type and returns a string based on the enum. That all compiles fine, but when I have do something like the following I get an error.
stuff.c
[cpp]
#include "main.h"
void doStuff()
{
char* stuff = getF(Foo.BAR);
}
[/cpp]
The error is:
[code]error: expected expression before 'Foo'[/code]
This is how I do enums in Java which works fine. :v:
I'm using enums so that it does give you an error if you try giving it an object that doesn't exist. If I just used a string, It wouldn't give a compile error for using a string that wouldn't be handled instead of a runtime error.
Shouldn't the "Foo" In your arguments be "foo"? - It's case sensitive.
[QUOTE=Tezza1234;16847742]Shouldn't the "Foo" In your arguments be "foo"? - It's case sensitive.[/QUOTE]
Oops that was just a mistake when I was typing it in Facepunch. Wasn't an error in my code I just simplified it for easier reading, so the error is still the same.
-snip-
Okay I got something that works, but it's really ugly.
[cpp]
Foo f = BAR;
char* stuff = getF(f);
[/cpp]
Is there anyway to not have to do the Foo f = BAR thing?
Enumerations in C and C++ aren't very good at all, it's almost pointless to name them.
Your enum:
[cpp]
typedef enum {
FOO,
BAR,
} Foo; [/cpp]
Simply introduces the type Foo and the two [B]global[/B] constants FOO and BAR.
[QUOTE=jA_cOp;16848060]Enumerations in C and C++ aren't very good at all, it's almost pointless to name them.
Your enum:
[cpp]
typedef enum {
FOO,
BAR,
} Foo; [/cpp]
Simply introduces the type Foo and the two [B]global[/B] constants FOO and BAR.[/QUOTE]
Oh, they're pretty useful in Java, thats why I was using them.
Is there a better way to get some kind of safety?
I thought about just using char* in the function arguments and then having #defines and define each string that should be passed to the function.
[QUOTE=PvtCupcakes;16848127]Oh, they're pretty useful in Java, thats why I was using them.
Is there a better way to get some kind of safety?
I thought about just using char* in the function arguments and then having #defines and define each string that should be passed to the function.[/QUOTE]
You can use namespaces, i.e:
[code]
namespace Foo
{
typedef enum {
FOO,
BAR,
} Foo;
}
void Test(Foo::Foo f)
{
// You can now do f == Foo::Bar
}
[/code]
[QUOTE=PvtCupcakes;16848127]Oh, they're pretty useful in Java, thats why I was using them.
Is there a better way to get some kind of safety?
I thought about just using char* in the function arguments and then having #defines and define each string that should be passed to the function.[/QUOTE]
No, there are no type-safe enums in C nor C++, unfortunately. Enums are pretty awesome in C#, Java and D.
I would simply do it like this:
[cpp]
typedef enum
{
FOO, /*enums start at 0 by default*/
BAR
} Foo;
static const char* FooStrings[] = {
"FOO",
"BAR"
};
const char* getFoo(Foo f)
{
return FooStrings[f];
}
[/cpp]
[QUOTE=CowsCanFly;16848208]You can use namespaces, i.e:
-snip-[/QUOTE]
I'm fairly sure he's using C, though.
[QUOTE=CowsCanFly;16848208]You can use namespaces, i.e:
[code]
namespace Foo
{
typedef enum {
FOO,
BAR,
} Foo;
}
void Test(Foo::Foo f)
{
// You can now do f == Foo::Bar
}
[/code][/QUOTE]
he's using C
[QUOTE=jA_cOp;16848236]No, there are no type-safe enums in C nor C++, unfortunately. Enums are pretty awesome in C#, Java and D.
I would simply do it like this:
[cpp]
typedef enum
{
FOO, /*enums start at 0 by default*/
BAR
} Foo;
static const char* FooStrings[] = {
"FOO",
"BAR"
};
const char* getFoo(Foo f)
{
return FooStrings[f];
}
[/cpp]
[/QUOTE]
With that method, I'd still have to do Foo f = BAR; right?
[QUOTE=PvtCupcakes;16848442]With that method, I'd still have to do Foo f = BAR; right?[/QUOTE]
Yes, BAR is still a global. That's how it works in C, sadly.
[cpp]
const char* strFoo = getFoo(BAR);
printf("strFoo: %s\n", strFoo);
[/cpp]
[QUOTE=jA_cOp;16848569]Yes, BAR is still a global. That's how it works in C, sadly.
[cpp]
const char* strFoo = getFoo(BAR);
printf("strFoo: %s\n", strFoo);
[/cpp][/QUOTE]
Actually, since you mentioned them being constants. I figured out you can do just do getF(BAR) instead of getF(Foo.BAR)
Just might need to change the names to be less ambiguous, like FBAR, FFOO, FETC.
Sorry, you need to Log In to post a reply to this thread.