0

I am working with OpenGL and I am trying to make a simple ground class that draws a rectangle.

I have a class called Vertex:

class Vertex
{
    public:
        Vertex(
            glm::vec3 _position,
            glm::vec3 _color = glm::vec3(0.0, 0.0, 0.0),
            glm::vec3 _normal = glm::vec3(0.0, 1.0, 0.0),
            glm::vec3 _texture = glm::vec3(0.0, 0.0, 0.0));

        void setNormal(glm::vec3 _normal);
        void setTexture(glm::vec3 _texture);
        virtual ~Vertex();
    protected:
    private:
        glm::vec3 position;
        glm::vec3 color;
        glm::vec3 normal;
        glm::vec3 texture;
};

And this is my Ground class:

class Ground
{
  private:
    double widht;
    double length;
    double y;
    int* indexes;
    Vertex* vertices;
  public:
    Ground(double _width, double_length, double y);
}

And here is what I want to do in the Ground constructor:

this->indexes = {0, 3, 1, 1, 3, 2};
this->vertices = {
        Vertex(glm::vec3(0 - width/2, y, 0-length/2)),
        Vertex(glm::vec3(0 - width/2, y, 0+length/2)),
        Vertex(glm::vec3(0 + width/2, y, 0+length/2)),
        Vertex(glm::vec3(0 + width/2, y, 0-length/2))
    }

I am getting this error when compiling:

error: cannot convert ‘<brace-enclosed initializer list>’ to ‘Vertex*’

Now I know I could hard-code initialize all of this, but I want a solution for a general ClassX* array header definition and constructor initialization.

2
  • If you use a pointer you have to allocate your own memory. I suggest you use a vector. Commented Dec 9, 2015 at 11:14
  • hmm... Well i also tried with the int* the following: this->indexes = new int[6]; this->indexes = { 0, 3, 1, 1, 3, 2}; //this didn't work this->indexes[0] = 0; this->indexes[1] = 3; // and so on this ^ worked but i don't understand why the first didn't and i want to understand the general concept. Please teach me how to allocate memory, after that i can use loops to initialize Commented Dec 9, 2015 at 11:24

2 Answers 2

2

vertices is a Vertex pointer, rather than an array. You can either make the member variable an array of fixed size or create an array of type Vertex on the heap.

for the first way something like this:

const int NUM_VERTS = 4;

class Ground{
.
.
.
Vertex vertices[NUM_VERTS]
.
.
.
};

Or if you want several different Ground objects with varying number of vertices you can use templates, but this will cause a big code bloat and executable size. Not a good idea, but it is possible

template<size n>
class Ground{
.
.
.
Vertex vertices[n];
.
.
.

}

otherwise, of course

vertices = new Vertex[4];
vertices[0] = Vertex(glm::vec3(0 - width/2, y, 0-length/2));
//etc
.
.
.
Sign up to request clarification or add additional context in comments.

4 Comments

trying this->vertices = new Vertex[4]; yelds the following error ground.cpp: In constructor ‘Ground::Ground(double, double, double)’: ground.cpp:25:34: error: no matching function for call to ‘Vertex::Vertex()’ this->vertices = new Vertex[4];
@user1840302 That's because you have no default constructor. Write one, even if it's an empty Vertex(){};
but i do have a constructor for Vertex : Vertex::Vertex( glm::vec3 _position, glm::vec3 _color, glm::vec3 _normal, glm::vec3 _texture) { this->position = position; this->color = _color; this->normal = _normal; this->texture = _texture; } with color, normal and texture having default values
Yes, but it's not a DEFAULT constructor if arguments are necessary. You need to at least provide one more for position. If you add another default argument for position in your existing constructor or write a new one with no args like Vertex(){}; it will work just fine, at the moment when invoking new the compiler can't find a suitable constructor
1

Change indexes (indices) and vertices to be std::vector<int> and std::vector<Vertex> respectively. Then you can write:

Ground::Ground(double _width, double _length, double y)
    : indexes{0, 3, 1, 1, 3, 2}
    , vertices{
        Vertex(glm::vec3(0 - width/2, y, 0-length/2)),
        Vertex(glm::vec3(0 - width/2, y, 0+length/2)),
        Vertex(glm::vec3(0 + width/2, y, 0+length/2)),
        Vertex(glm::vec3(0 + width/2, y, 0-length/2))
    }
{
}

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.