3

I was having a problem getting a function to accept an enum as a return-type. In the code below there's an enum:

Status{ DEAD, WOUNDED, HEALTHY }

and a function with Status as a return type:

Status getStatus();

Header Code:

class Discovery
{
public:
    void doCombat( int );
    void setCombatThreshold( int );
    void setHealth( int );
    void setStatus( int );
    Status getStatus();
private:
    enum Status { DEAD, WOUNDED, HEALTHY };
    Status charStatus;
    int yourHealth;
    int combatThreshold;
};

Initially the associated function definition read:

Status Discovery::getStatus()
{
    switch ( charStatus )
    {
    case DEAD:
        return DEAD;
        break;
    case WOUNDED:
        return WOUNDED;
        break;
    case HEALTHY:
        return HEALTHY;
        break;
    };
}

I found this answer: returning enum from function in C++ base class which helped me realize I really needed the first line of the function to read:

Discovery::Status Discovery::getStatus()

But, I was still receiving a 'missing type specifier' error for my header code. I realized that having my 'enum Status' declaration under the private access-specifier might be making the difference, so I moved it to the public access-specifier in my header code. It worked! But I'd like some elucidation on why it wouldn't work under the private access-specifier. What I've managed to find elsewhere is:

Objects of the class cannot access private data members.

My interpretation of what happened being - with the enum-type definition under the private access-specifier, the function (and eventually an object calling that function) couldn't possibly access 'understand' my enum-type, and therefore accept it as a return type.

But - if that's the case, why am I allowed to return variables declared under the private access-specifier with the same problem? Is it because their types are already understood elsewhere, and so the program has no problem accepting them?

Community
  • 1
  • 1
Azoreo
  • 236
  • 1
  • 3
  • 14
  • Others have answered why you're having the compilation error, but there is another issue here. Declaring the `Status` enumeration as private and then having it be the return type of a public function doesn't make any sense. No one who instantiates the `Discovery` class can call `Discovery::getStatus` because the return type is inaccessible to them. – Praetorian Aug 05 '11 at 20:22
  • They can call it, but the only thing they can do with it is pass it to a function that has access to the Status definition. (Discovery or a friend function/class, or derived class) – Mooing Duck Aug 05 '11 at 20:27
  • 1
    Also, getStatus can be simplified to `Discovery::Status Discovery::getStatus() {return charStatus;}` – Mooing Duck Aug 05 '11 at 20:28

2 Answers2

4

Status is defined after it's used. Move status up before the get method.

If you plan to use the enum outside of the class you will want to move the enum into public scope.

Nico
  • 3,826
  • 1
  • 21
  • 31
  • I tested this by just placing the private declarations above the public declarations, and it worked. Well, that makes me blush. I had to come up with some crazy understanding, and it was something so simple. Thanks for the help. – Azoreo Aug 05 '11 at 20:20
  • 1
    @Nico Being able to return the private enum has nothing to do with the fact that enums are ints. I can declare a private nested class and return that from a public function too. It is allowed because the private object is visible within the class declaration. However, other than friend functions, no one will be able to call the public function and store the return value. – Praetorian Aug 05 '11 at 20:40
4

You need to change the order of the declarations. Since you're using Status prior to it being declared, it's giving you an error. You have to move the declaration of Status up.

class Discovery
{
private:
    enum Status { DEAD, WOUNDED, HEALTHY };
public:
    void doCombat( int );
    void setCombatThreshold( int );
    void setHealth( int );
    void setStatus( int );
    Status getStatus();
private:
    Status charStatus;
    int yourHealth;
    int combatThreshold;
};
Dave S
  • 20,507
  • 3
  • 48
  • 68
  • This might be true but is not what the question is about. –  Aug 05 '11 at 20:18
  • 1
    The question is 'Why does the first one fail, and why does the second one succeed?'. The OP assumes it has to do with the public/private definition. That is incorrect. The problem was because status was declared after the public function, and it was causing the error he was seeing. – Dave S Aug 05 '11 at 20:19
  • It should be noted that if Status is less visible than getStatus(), then the caller cannot save it, compare it, or do ANYTHING with it besides pass it to another function in Discovery. (of which, there are no functions that take Status, making getStatus() unusable. – Mooing Duck Aug 05 '11 at 20:21
  • Well, it's not useful, because it makes it hard to use the return value, but it's legitimate to call it without storing the return value, or pass it directly to a function that does have permission (such as a friend function). I'm not sure I WOULD do such a thing, but it's legal. – Dave S Aug 05 '11 at 20:24
  • @Dave, on second read, I see you are right about the OP's intentions. Sorry and +1. –  Aug 05 '11 at 20:30
  • You can store it alright: `int result = foo.getStatus();` :) Not particularly useful, though, because apparently the caller is not supposed to be able to interpret the result. - BTW, in C++0x you might also be able to keep the type: `auto result = foo.getStatus();` – UncleBens Aug 05 '11 at 23:16
  • The `int` I expected, but I tried the `auto` in 4.5.2 and it also let me assign it to a local variable. – Dave S Aug 06 '11 at 01:15