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.
2 Answers 2
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.
1 Comment
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.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.
CState::eState CState::eGet(){ return Current; }