views:

172

answers:

1

Right now, my project has two classes and a main. Since the two classes inherit from each other, they are both using forward declarations. In the first object, right underneath the #include statement, I initialize two enums, before the class definition. I can use both enums just fine inside that class. However, if I try to use those enums in the other class, which inherits from the first one, I get an error saying the enum has not been declared. If I try to redefine the enum in the second class, I get a redefinition error.

I have even tried using a trick I just read about, and putting each enum in its own namespace; that didn't change anything.

Here's an example:

#ifndef CLASSONE_H
#define CLASSONE_H

namespace Player
{
    enum Enum
    {
        One,
        Two,
    };
}

#endif

Then inside the second class, I attempt to use the enum declared earlier:

void AddPlayer(Player::Enum playerNumber);

and instead get an error saying 'Player' has not been declared.

+3  A: 

I'm not sure what issue you are having without seeing your code, but this compiles:

enum OutsideEnum
{
    OE_1,
    OE_2,
};

namespace ns
{
    enum NSEnum
    {
       NE_1,
       NE_2,
    };
}

class Base
{
public:
    enum BaseEnum
    {
        BE_1,
        BE_2,
    };

    void BaseFunc();
};

class Derived
{
public:
    enum DerivedEnum
    {
        DE_1,
        DE_2,
    };

    void DerivedFunc();
};

void Base::BaseFunc()
{
    BaseEnum be = BE_1;
    Derived::DerivedEnum de = Derived::DE_1;
    OutsideEnum oe = OE_1;
    ns::NEEnum ne = ns::NE_1;
}

void Derived::DerivedFunc()
{
    Base::BaseEnum be = Base::BE_1;
    DerivedEnum de = DE_1;
    OutsideEnum oe = OE_1;
    ns::NEEnum ne = ns::NE_1;
}

int main()
{
    Base::BaseEnum be = Base::BE_1;
    Derived::DerivedEnum de = Derived::DE_1;
    OutsideEnum oe = OE_1;
    ns::NEEnum ne = ns::NE_1;
}

Two things to watch for with enums defined inside a class definition:

  1. Make sure it's declared public if you want it publicly available.
  2. When referencing it from anywhere other than the class it's defined in, use the class name to qualify the name of the enum and the values.

EDIT:

Ok, the problem has nothing to do with enums, but rather order of inclusion, when you have a base class and a derived class, only the derived class needs to know about the base class:

Base class header:

#ifndef BASE_H
#define BASE_H

enum BaseEnum
{
};

class Base
{
};
#endif

Derived class header:

#ifndef DERIVED_H
#define DERIVED_H

#include "Base.h"

class Derived
{

   void Func(BaseEnum be);
};
#endif
Eclipse
Gotcha. The problem was that I wrote the derived class before I wrote the manager for it, so I declared the enum in the derived class's file even though the base class needed it as well. I thought a forward declaration would be in order, but that wasn't the case. I was able to get the code to compile by removing the #include "classtwo" and moving the enum to the base class (while leaving in the forward declaration so classone could have a vector of classtwo).
VGambit