Initialize vector size

Hello, Getting back into programming after a few years off and a bit rusty.

My question is: Is this going to initialize the size of the vector array's position and color properly?

#include <GLFW/glfw3.h>
#include <vector>


class TerrainClass
{

private:

struct VertexType
{
std::vector<float> position[3];
std::vector<float> color[4];
};

public:

TerrainClass(void);
~TerrainClass(void);
nah, you've defined 2 separate vectors there.
Actually they are 7 vectors.
You're making an array of vectors, just like this makes an array of ints:

 
int i_array[3];


What you want to do, is declare your vectors normally (without the []) and then initialize them in the constructor:

1
2
3
4
TerrainClass::TerrainClass() {
position.resize(3);
color.resize(4);
}
The std::vector has multiple constructors: http://www.cplusplus.com/reference/vector/vector/vector/

Should those vectors have some default values too?
Actually they are 7 vectors.

Oh yea :)
Thanks very helpful. Seems to work now. This is what I have done: ..........Using OpenGL and G++, linux

class TerrainClass
{

private:

struct VertexType
{
std::vector<float> position;
std::vector<float> color;
};
<code snip>

bool TerrainClass::InitializeBuffers(GLFWwindow* window)
{

VertexType* vertices;
unsigned long* indicies;
int index, i, j;
float positionX, positionZ;

vertices = new VertexType[m_vertexCount];
positionX = (float)i;
positionZ = (float)(j+1);

vertices[index].position = {positionX, 0.0f, positionZ};
<code snip>

I also had to include a compilier flag for std=gnu++11 to load the vector.

Topic archived. No new replies allowed.