Current Problem:
I need to access an enum from a class that is forward declared, similar to this situation:
Human.h
#include "Dog.h"
class Human{
public:
enum Language: uint32_t {
None = 0,
English = 1,
Japanese= 2,
};
}
Dog.h
class Human;
class Dog{
void understand(Human::Language speech);
}
Dog.cxx
#include "Dog.h"
#include "Human.h"
void Dog::understand(Human::Language speech) {
// Do stuff with Human::Language
return;
}
Errors:
The IDE tells me Dog.cxx's implementation is not compatible with Dog.h's deceleration, citing the enum as <erro-type> in the error hint (only red squiggle)
When compiling, any mention of the enum in Dog.h/c.xx throws errors, not being able to find the enum
Extra Info:
MSVC 15 2017
Full architecture of program requires the enum to be accessible like this
Forward Deceleration is mandatory in order to solve a circular dependency within my program that is not seen here
Basically the answer is no, you can't forward declare the enum in this context. Your choices are:
Move the enum into a lightweight base class.
struct HumanBase
{
enum Language: uint32_t {
None = 0,
English = 1,
Japanese= 2,
};
};
//fwd declare
struct Human;
Move the enum out of the class.
enum HumanLanguage : uint32_t {
None = 0,
English = 1,
Japanese= 2,
};
struct Human;
and then if you need to later on, you can do:
struct Human
{
typedef HumanLanguage Language;
};
Change all the method that use the enum to be a template (which may work in some cases, possibly not in others)
class Dog{
template<typename LanguageType)
void understand(LanguageType speech);
};
Related
So I am currently working on a text-based RPG, and I have run into an odd issue. In working on the weapon coding, I chose to go with enums for the type and rarity of the weapon. I have already programmed everything for my Weapon class; yet, when I try to create a Weapon object, I get an error having to do with my enums -- error: 'common' is not a type. The relevant code is as follows:
In Enum_Weapon.h:
#ifndef ENUM_WEAPON_H_INCLUDED
#define ENUM_WEAPON_H_INCLUDED
enum rarity{common, uncommon, rare, epic, legendary};
enum weaponType{axe, bow, crossbow, dagger, gun, mace,
polearm, stave, sword, wand, thrown};
#endif // ENUM_WEAPON_H_INCLUDED
And in Weapon.h:
#ifndef WEAPON_H
#define WEAPON_H
#include "Item.h"
#include "Enum_Weapon.h"
class Weapon : public Item{
public:
Weapon();
Weapon(rarity r, weaponType t, std::string nam, int minDam,
int maxDam, int stamina = 0, int strength = 0,
int agility = 0, int intellect = 0);
The code, of course, goes on; but this is all the code that is relevant to my error. Finally, when I try to create a Weapon object, I get the error:
#ifndef LISTOFWEAPONS_H
#define LISTOFWEAPONS_H
#include "Weapon.h"
#include "Enum_Weapon.h"
class ListOfWeapons
{
public:
ListOfWeapons();
protected:
private:
Weapon worn_greatsword(common, sword, "Worn Greatsword", 1, 2);
};
#endif // LISTOFWEAPONS_H
The same error also happens with the sword enum. I have researched the problem, but I can't find anything similar to the issue that I am having. Any help is much appreciated!
Your weapon attribute was a function declaration, not a variable definition. You must pass in default values in the constructor.
class ListOfWeapons
{
public:
ListOfWeapons() :
worn_greatsword(common, sword, "Worn Greatsword", 1, 2)
{
//...constructor stuff
}
protected:
private:
//function decl
//Weapon worn_greatsword(common, sword, "Worn Greatsword", 1, 2);
Weapon worn_greatsword;
};
I need call a method with this signature in my Manager class:
void createPlayer(Player& player, PlayerType& playerType);
I have a Player defined like so:
using namespace std;
enum PlayerType { FORWARD, DEFENSEMAN, GOALIE };
class Player {
public:
Player();
void setType(PlayerType);
private:
PlayerType type;
};
This is how I try to call the method in main ...
#include "Player.h"
#include "Manager.h"
int main() {
Manager manager;
Player player;
PlayerType t = PlayerType::FORWARD;
manager.createPlayer(player, t);
return 0;
}
... but it fails to compile with this error:
Main.cc: In function ‘int main()’:
Main.cc:12:18: error: ‘PlayerType’ is not a class or namespace
Any ideas? Note: I cannot change the signature of the createPlayer method.
enum doesn´t create a namespace.
Therefor PlayerType t = PlayerType::FORWARD; should be changed to:
PlayerType t = FORWARD;
Notice that c++11 introduce enum classes, which have a namespace. Beside this MSVC has an extension which treats (regular) enums like they have namespace. So your code should actually work with MSVC.
Unfortunately enum by default doesn't create an enum namespace. So when declaring:
enum PlayerType { FORWARD, DEFENSEMAN, GOALIE };
you'll have to use it like this:
auto x = FORWARD;
Thankfully, though, C++11 introduced enum class or enum struct to solve this issue:
enum class PlayerType { FORWARD, DEFENSEMAN, GOALIE };
You'll then be able to access it like you did:
auto x = PlayerType::FORWARD;
Your error seems to be in this line:
PlayerType t = PlayerType::FORWARD;
As far as I know, the scope resolution operator (::) is not valid on regular C++98 enums unless your compiler supports them through non-standard extensions (like Visual Studio). Therefore you can't use it to reference an specific value from the enumerator.
Also, C++98 enums have the problem of polluting the namespace in which they are defined which can lead to name clash. Luckily C++11 solved this introducing the enum class. For more information check Stroustrup's FAQ: http://www.stroustrup.com/C++11FAQ.html#enum
add a static function say getForward:
class Player {
public:
Player();
void setType(PlayerType);
static PlayerType getForward {
return FORWARD;
}
private:
PlayerType type;
};
Then use it in main:
manager.createPlayer(player, Player::getForward());
This should work.
class Shapemaker
{
public:
static Shape * shapeCreate(CDrawView::shape sh);
};
My enum on my CDrawView class is
enum shape{line, rect, elli};
shape current_shape;
when i call Shapemaker::shapeCreate(current_shape) on I get error c2653CDrawView : is not a class or namespace name on shapemaker.h
This is probably the most plain thing to do:
class Shapemaker{
public:
enum Color { //your colors here }
};
class Otherclass{
void fun(Shapemaker::Color);
};
Now if your compiler does not recognize Shapemaker as a class name, that makes me think you didn't include its header file before declaring Otherclass.
If it's an enum member of the other class, then you can reference it as nameoftheClass::Color, but it'd have to be publicly-visible:
void function(nameoftheClass:Color input);
Say you have as follows:
class C {
public:
enum E {
HERP,
DERP
};
};
A function taking that enum would look like:
void foo(C::E e) {
// do stuff with e
}
It's all a matter of namespace in the end. Have a look at the answers to this question too, definitively you must not use the keyword enum in the function's parameter list, use directly the enum name with the appropriate namespace.
namespaces for enum types - best practices
i tried to pass the color variable enum, but it gives me a compile error saying that the "nameoftheclass" is not a class or namespace
You need to have a declaration placed before you use it, meaning you need proper header files:
MyClass.h
class MyClass {
public:
enum Color {
Red,
Green,
Blue
};
};
MyOtherClass.h
#include "MyClass.h" // This is required.
// Now you can use MyClass::Color freely.
I have a class in which I have an enumeration, defined like this:
class X
{
public:
enum Direction {DIR_LEFT, DIR_RIGHT};
};
Now I want this enumeration to be reused in another class, like this:
class Y
{
public:
typedef X::Direction Direction;
};
As expected, using Y::Direction works correctly, e.g.:
void myFunction (Y::Direction dir)
{
}
But the values within the enumeration does not seem to be 'copied' together with the typedef. If I write the following, I get compilation errors:
myFunction (Y::DIR_LEFT);
Instead, I have to refer to the original place of the enumeration again, like this:
myFunction (X::DIR_LEFT);
Which defeats my purpose of typdefing the enumeration.
The only solution I see is to move the enumeration out of class X, and putting it in another class (e.g. MyEnums), so it can be reused by X and Y (although they should still use MyEnums::DIR_LEFT and MyEnums::DIR_RIGHT), but at least the code does not depend on class X anymore.
Why are the enumeration values itself no exposed via the typedef?
Are there any other patterns to manage enumerations in different classes?
Unfortunately C++ doesn't introduce a new scope with an enum although C++0x is improving things.
Practically this means that you can't typedef an enum and get the enumerated values as well.
What you can do is use a nested struct with the name you want for the enum and typedef THAT.
class X
{
public:
struct Direction { enum EnumType {LEFT, RIGHT}; };
};
class Y
{
public:
typedef X::Direction Direction;
};
Now you can do:
myFunction (Y::Direction::LEFT);
The purpose of the nested struct is to create a "fake" scope to holld both the enum name and its values.
Here is my understanding of how enums work in C++. (Or at least my observed behaviour of enums in Microsoft Visual C++.)
The enum keyword does not create a scope the same way that classes do.
The full name then for your enum 'Direction', is X::Direction. The values within that enum are still part of the class scope, so they are X::DIR_LEFT and X::DIR_RIGHT.
When you typedef the enum in another class, this does not change the scope of the values of the enum.
I suggest you put the enum inside a namespace in a header file if you want to share it in multiple locations.
If you want the enum values to be members of both classes, the
solution is to define a separate class with the enum, and
inherit from it, e.g.:
class MyEnums
{
protected:
~MyEnums() {} // Prevent delete through pointer to this class
public:
enum Direction
{
DIR_LEFT,
DIR_RIGHT
};
};
class X : public MyEnums
{
// ...
};
class Y : public MyEnums
{
// ...
};
Users will see X::Direction, X::DIR_LEFT and Y::Direction,
Y::DIR_LEFT. Of course, they'll still be able to pass
a Y::DIR_LEFT to a function expecting an X::Direction; to
prevent that, make MyEnums a template, with the derived class as
the template argument.
Anything shared by more than one class should be factored outside of the classes and perhaps into a parent class.
direction.hpp:
#ifndef DIRECTION_HPP
enum Direction {DIR_LEFT, DIR_RIGHT};
#endif
x.hpp:
#ifndef X_HPP
#include "direction.hpp"
class X
{
Direction dir;
};
#endif // X_HPP
y.hpp
#ifndef Y_HPP
#include "direction.hpp"
class Y
{
Direction dir;
};
#endif // Y_HPP
If the original declaration:
class X
{
public:
enum Direction {DIR_LEFT, DIR_RIGHT};
};
is embedded in a large legacy code-base, then we might want a solution that does not change any existing uses of X::Direction. In that case, the rather ugly:
class Y
{
public:
typedef enum X::Direction Direction;
static const enum X::Direction DIR_LEFT = X:DIR_LEFT;
static const enum X::Direction DIR_RIGHT = X:DIR_RIGHT;
}
works...
Definitely not recommended for new code, however!
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.
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:
Make sure it's declared public if you want it publicly available.
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