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.

shieldfoss
  • 886
  • 2
  • 12
  • 22

2 Answers2

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.

Community
  • 1
  • 1
Mike Seymour
  • 249,747
  • 28
  • 448
  • 644
  • @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. – Mike Seymour Jul 28 '14 at 11:07
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.

Some programmer dude
  • 400,186
  • 35
  • 402
  • 621