0

I have a simple state machine I am trying to implement. However, when I get a peculiar error:

State.cp:7:1: error: 'eState' does not name a type
 eState CState::eGet(){
 ^

eState is an enumerator from the class CState:

#ifndef __STATE_H
#define __STATE_H
#include <string>
class CState {
 public:
 enum eState {
 eData,
 eInterface,
 ePresentation,
 eExit
 };

And it is (Currently, non-functionally) returned like so:

private:
 eState Current;
public:
 estate eGet();

where eGet() is defined like so:

eState CState::eGet(){
 return Current;
};

I am trying to use .eGet() as a switch value but my main function tells me the enum values aren't "declared in this scope"

Now obviously, I could just move the enumerator to a "Common.h" file and have everybody #include that, but it quite clearly belongs to the state class, so I'd prefer to keep it there.

asked Jul 28, 2014 at 10:58
1
  • 1
    Do you need to give the return type an explicit namespace? CState::eState CState::eGet(){ return Current; } Commented Jul 28, 2014 at 11:02

2 Answers 2

3

The enumeration is scoped inside the class, but the return type of the function definition is not. So you'll need to specify the scope:

CState::eState CState::eGet(){
 return Current;
} // NOTE: no ; here

The function body, parameter list and trailing return type (if present) are within the class scope, so you don't need the extra qualification there. So, since C++11, you could also define the function thusly:

auto CState::eGet() -> eState {
 return Current;
}

Also, you shouldn't use reserved names like __STATE_H. It could bite you later.

answered Jul 28, 2014 at 11:01
Sign up to request clarification or add additional context in comments.

1 Comment

@medivh: Indeed, main is also not within the class scope, so you'll need to specify the scope there as well: case CState::eData: You can only use unqualified names within their scope. In this case, that's the scope of the class, which is (roughly) the class definition itself, and its member function definitions.
2

In the source file there is no name eState in the global scope, you have to use the scoping operator :: to tell the compiler which scope the symbol is in:

CState::eState CState::eGet(){...}

And no, the compiler is not required to know that you meant CState::eState, even though it probably could have figured it out.

answered Jul 28, 2014 at 11:01

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.