C++11 - enum class

Trying to get this enum class working, but keep getting
'does not name a type' error.

1
2
3
4
5
6
7
class A
{
    public:
        enum class States {current, taco, burrito};

        virtual States stateChange() = 0;
};


Also claiming 'error: expected identifier before ',' token' and then points to the second comma in my enum list.

I've never really used enums as I haven't had much use for them before.

Sorry for the lack of pastes btw, I'm working on two separate computers and for some reason didn't think of opening Chrome on the computer with the code -_-
Sounds like it might be something that occurs (or doesn't occur) before that line of code is reached. What compiler are you using?

http://ideone.com/vUKb0R
Hmm, I'm on GCC 4.8.1

Not really sure why it's not working. Would it have anything to do with it being in a header file?
https://gist.github.com/anonymous/8063627

No idea here. The 'does not name a type' is gone, but still getting the expected identifier error. I'm sure I'm overlooking something dumb here.

Wow figured. The include guard in the file using this class was missing a letter -_-
Last edited on
Wow figured. The include guard in the file using this class was missing a letter -_-


That should be really easy to spot. "#define" and "#ifndef" have exactly the same number of letters, so the symbol you're using for your header guard should line up exactly in those two lines:

1
2
#ifndef _MY_HEADER_INCLUSION_GUARD_SYMBOL_
#define _MY_HEADER_INCLUSION_GUARD_SYMBOL_ 


See? It makes it really easy to see if there's a spelling difference between the two.

Of course, there's no reason not to use copy-and-paste for this anyway, so there shouldn't be any risk of making them different in the first place.
Last edited on
Yeah I know, I just wasn't looking at the header guards. Tunnel vision, ya know :)
And this is why you should start using #pragma once instead.

I always did think header guards are hackery: a nice preprocessor trick to deal with poor design.
Then again, #pragma once is nonstandard (as far as I know), and should probably be avoided for portability.

Though, having thought about it, I can't actually think of any compilers off the top of my head that DON'T support it...
NT3 wrote:
I can't actually think of any compilers off the top of my head that DON'T support it...

It doesn't do the same thing as header guards if the header is copied or linked. It is also very hard to specify portably, since it deals with filesystem-level concepts (inode, FileIndex, etc)
I usually have both #define/#ifndef and #pragma once, simply because visual studio 2010 doesn't want to compile files which don't have either #include something or #pragma something - and I often have small objects which don't require includes.
Topic archived. No new replies allowed.