Yash Ahuja
Yash Ahuja

Reputation: 1

C++ Using an enum from another class properly

I'm currently implementing some code using OpenGL and I've created wrappers for various OpenGL classes. In particular, I have a buffer class that can represent either a vertex array buffer or an element array buffer. To do this, I declared my class in the header like so:

Buffer.h

namespace FE { namespace GL {
    enum BufferType {
        ARRAY = GL_ARRAY_BUFFER,
        ELEMENT = GL_ELEMENT_ARRAY_BUFFER
    };

    class Buffer {
    public:
        Buffer(BufferType type);
        ... rest of class ...
    };
}

In my Renderer class, I try to instantiate some buffers as class members:

Renderer.h

...
#include "../GL/Buffer.h"

namespace FE { namespace Render {
    class Renderer {
    ...
    private:
        GL::Buffer vbo(GL::BufferType::ARRAY);
        GL::Buffer element(GL::BufferType::ELEMENT);
    };    
}}

For some reason, attempting to use the enum this way gives me the errors "syntax error: identifier 'ELEMENT'". Intellisense also warns that "constant 'FE::GL::ELEMENT' is not a type name."

I'm not quite sure why this doesn't work, as before while testing my buffer related code I created buffers in a similar manner (by accessing the enum values with the scope operator).

Any help on how to fix this issue would be appreciated.

Upvotes: 0

Views: 3202

Answers (2)

Holt
Holt

Reputation: 37686

If you want to default initialize member, you need to use either braces {} or =:

Non-static data members may be initialized in one of two ways:

2) Through a default member initializer, which is simply a brace or equals initializer included in the member declaration, [...]

namespace FE { namespace Render {
    class Renderer {
    private:
        GL::Buffer vbo{GL::BufferType::ARRAY};
        GL::Buffer element{GL::BufferType::ELEMENT};
    };    
}}

These restriction were included in order to avoid amibiguity between member function declaration and default member initializer, see the proposal: http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2008/n2756.htm (in particular the section Problem 1).

Upvotes: 2

Chris
Chris

Reputation: 1244

An enum does not create a scope in C++. The type of the enum is FE::GL::BufferType but for using it you just type GL::Buffer vbo(GL::ARRAY);

If you want a scoped enum (using C++11) use an enum class

Upvotes: 3

Related Questions