0

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.

3
  • are you trying to initialize vbo and element data members, or are you trying to create vbo and element member functions ? Commented Jul 8, 2016 at 9:11
  • I'm trying to create data members, not member functions. Commented Jul 8, 2016 at 9:13
  • 1
    Oh, I see what you're getting at. I fixed it now by changing it to: GL::Buffer vbo = GL::Buffer(GL::ARRAY); Thanks a bunch! Commented Jul 8, 2016 at 9:17

2 Answers 2

3

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

Sign up to request clarification or add additional context in comments.

4 Comments

In C++11 and later, GL::BufferType::ARRAY is perfectly valid, and since this question is tagged C++14, this is not the problem.
In case it does, I have not heard about it. What has changes in C++11 and later regarding the scope of an enum? They provided the enum class for scoped enums as I already said but I dont know of anything that changed for the plain old enums.
See the original proposal(section 3.2), the corresponding section on Bjarne's C++11 FAQ and the section 7.2/11 of the standard (in particular the example with direction). I have to admit it is hard to find a good quote for this in the current standard.
The correct quotes from the standard are 3.4.3/1: "The name of a class or namespace member or enumerator can be referred to after the :: scope resolution operator (5.1) applied to a nested-name-specifier that denotes its class, namespace, or enumeration." and 5.1/12: "A nested-name-specifier that denotes an enumeration (7.2), followed by the name of an enumerator of that enumeration, is a qualified-id that refers to the enumerator. The result is the enumerator." (numbering are from n4296)
2

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).

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.