C++ enum declaration after use in class - c++

I tried to create a class with an enum, and a function that returns a variable of this enum. But I ran into a problem:
This works:
class SizeBoxClass {
public:
enum BoxType{xBox, yBox};
BoxType intersects() {
return xBox;
}
}SizeBox;
But this does not:
class SizeBoxClass {
public:
BoxType intersects() {
return xBox;
}
enum BoxType { xBox, yBox };
}SizeBox;
I get "BoxType is undefined" at the line of the function declaration...
Why? I thought declaring a class member after its use was not a problem.

enum BoxType { xBox, yBox }; is a type declaration (and also definition), not a member variable or member function.
Types need to be declared before first use.

Related

enum type declared inside a class access confusion

In MyClass below, enum MyType is defined inside the class.
In main, I create a variable of MyClass::MyType t. This compiles fine. However, when I wish to assign it a value such as OPEN, there is a compilation error "OPEN was not declared in this scope"
Firstly it probably doesn't make sense declaring an enum type inside the class and limiting its scope there and then creating a variable of that enum type elsewhere, but I'm just trying to understand what's happening.
In the first place, how am I able to create a variable of MyType in main when an object hasn't even been created? Are enums and struct types defined in a class like that implicitly static?
Also, the compiler has access to the enum code, so why doesn't it understand "OPEN"? Thanks
#include <iostream>
using namespace std;
class MyClass
{
public:
enum MyType
{
OPEN,
CLOSED
};
struct MyStruct
{
int val1;
int val2;
};
};
int main()
{
MyClass::MyType t;
t = OPEN; // compilation error
return 0;
}
Your enum MyType is inside the class, so its values are expected to be accessed through the class and the enumeration. You are already creating a MyType without instantiating the class, but an example of instantiation through the class is also provided.
class MyClass
{
public:
enum MyType
{
OPEN,
CLOSED
};
struct MyStruct
{
int val1;
int val2;
};
};
int main()
{
MyClass::MyType t; // Already a MyType value!
MyClass c; // Building your class
t = MyClass::MyType::OPEN; // No compilation error
t = c.OPEN; // Accessing enum through instantiated class
return 0;
}
Like Remy said. Value OPEN is a part of MyClass class and is only reachable in that classes scope. For your compiler to see it and use it you need to acces it through MyClass::OPEN.
(In addition to what others wrote)
If supported by the compiler (Since C++ 11),
it is a better practice to use enum class:
enum class MyType
{
OPEN,
CLOSED
};
"The enum classes (“new enums”, “strong enums”) address three problems with traditional C++ enumerations:
1) Conventional enums implicitly convert to an integer, causing errors when someone does not want an enumeration to act as an integer.
2) Conventional enums export their enumerators to the surrounding scope, causing name clashes.
3) The underlying type of an enum cannot be specified, causing confusion, compatibility problems, and makes forward declaration impossible."
ISOCPP FAQ - enum class
-
In that case, use the syntax:
int main()
{
MyClass c;
MyClass::MyType t;
t = MyClass::MyType::OPEN;
return 0;
}
See [decl.enum]/11:
Each enum-name and each unscoped enumerator is declared in the scope
that immediately contains the enum-specifier. Each scoped enumerator
is declared in the scope of the enumeration. These names obey the
scope rules defined for all names in [basic.scope] and [basic.lookup].
[ Example:
enum direction { left='l', right='r' };
void g() {
direction d; // OK
d = left; // OK
d = direction::right; // OK
}
enum class altitude { high='h', low='l' };
void h() {
altitude a; // OK
a = high; // error: high not in scope
a = altitude::low; // OK
}
— end example ] An enumerator declared in class scope can be referred
to using the class member access operators (​::​, . (dot) and ->
(arrow)), see [expr.ref]. [ Example:
struct X {
enum direction { left='l', right='r' };
int f(int i) { return i==left ? 0 : i==right ? 1 : 2; }
};
void g(X* p) {
direction d; // error: direction not in scope
int i;
i = p->f(left); // error: left not in scope
i = p->f(X::right); // OK
i = p->f(p->left); // OK
// ...
}
— end example ]

Why am I getting this 'enum' is not a class or a namespace error?

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.

How to make a function that receives an enum

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.

Structure initialization in class objects

How do I initialize the structure variables of type class objects? I have the following code:
#include<iostream>
using namespace std;
class bitmap {
public :
bitmap() { clear() ;}
get();
set();
clear();
static const int a=10;
};
bitmap::get() {
};
struct bitmap_list {
bitmap_list_value _value;
}
int main()
{
bitmap bitmap_list_value;
bitmap_list bbbb;
bbbb. _value=bitmap_list_value.a;
cout << bbbb._value << endl;
}
Is this code correct, or is it possible to initialize the structure containing the class objects? This is the error I receive:
>error: ‘struct error: ‘_bitmap_list_value’ does not name a type
>error:bitmap_list’ has no member named ‘_value’
No, this code is not correct. You're referencing a type bitmap_list_value which is never declared.
Based on your comment ("bitmap_list_value is object of class bitmap"), it sounds as if you also have this, but haven't included it in your question's code for some reason:
typedef bitmap bitmap_list_value;
But yes, of course you can include members of class types inside structs. A struct is more or less a class with all fields made public by default, you can define methods inside structs just as you can with classes, and so on.

typedef'ing an enum does not make the enum-values visible

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!