PDA

View Full Version : vector



mickey
26th April 2006, 10:14
HI, I need a vector such that every elemet is an array of 4 elements; I try this....but I don't sure it works....but then must use in setLight


std::vector <GLfloat>position[4];
position->push_back(1);
position[0][0] = 0.0f;
position[0][1] = 1.0f;
position[0][2] = 2.0f;
position[0][3] = 0.0f;
glLightfv(GL_LIGHT0, GL_POSITION, position[0]); //this don't compile

jcr
26th April 2006, 15:04
vould


std::vector<std::vector<int> > cont;
std::vector<int> temp;
temp.push_back(1);
temp.push_back(2);
temp.push_back(3);
temp.push_back(4);
cont.push_back(temp);
int k;
k = cont[0][0];

do the trick?

jacek
26th April 2006, 15:20
glLightfv(GL_LIGHT0, GL_POSITION, position[0]); //this don't compile
It's because OpenGL doesn't know anything about STL containers --- it's a C library.

Since std::vector keeps all elements in a single block, you can try:
glLightfv( GL_LIGHT0, GL_POSITION, &(position[0][0]) );

mickey
26th April 2006, 22:47
It's compile! But I see that sometimes the instructions below get 'debug error'...What's the problem?


std::vector <GLfloat>position[4];
position->push_back(1);
position[0][0] = 0.0f;
position[0][1] = 1.0f;
position[0][2] = 2.0f;
position[0][3] = 0.0f;

jacek
26th April 2006, 23:01
std::vector <GLfloat>position[4];
I hope you realize that this is an array of 4 vectors, not vector of 4-element arrays.

mickey
26th April 2006, 23:12
I suspected this; but in first post you 'didn't correct me'... and I was believing that!
Re-post first question; I need a <vector> such that every element is an array[4];
---
In realty, I have declare vector<GLfloat> pos[4] inside a class declaration! So I think it's OK(?). I can't create instance in class declaration(???).

jacek
27th April 2006, 00:18
I need a <vector> such that every element is an array[4];
Then try this:
std::vector< GLfloat[4] > position;

mickey
27th April 2006, 16:26
hi, this below compile but I've problem to insert element:


std::vector <GLfloat[4]> pos;
std::vector <GLfloat[4]> pos(1); //don't compile
pos.push_back(?); how to insert element?

jacek
27th April 2006, 17:48
hi, this below compile but I've problem to insert element:

std::vector< GLfloat[4] > pos;
GLfloat p[4];
p[0] = 0.0f;
p[1] = 1.0f;
p[2] = 2.0f;
p[3] = 3.0f;
pos.push_back( p );

mickey
27th April 2006, 23:42
Hi, I've alreadt tried this but compiler says me


C:\Program Files\Microsoft Visual Studio .NET 2003\Vc7\include\vector(810): error C2440: 'initializing' : cannot convert from 'const GLfloat [4]' to 'float [4]'


If I change to this:


std::vector <float[4]> position;
float pos[4];
pos[0] = 0.0;
pos[1] = 1.0;
pos[2] = 0.0;
pos[3] = 0.0;
position.push_back(pos);

compiler get me the same error.....?!

wysota
27th April 2006, 23:51
Then use the const_cast to get rid of "const" or create your arrays on heap:


std::vector <GLfloat*> position;
GLfloat *pos new GLfloat[4];
pos[0] = 0.0;
pos[1] = 1.0;
pos[2] = 0.0;
pos[3] = 0.0;
position.push_back(pos);

And remember to delete[] the array when you no longer need it to avoid memory leaks.

Or better yet, make your own class, something like:


struct MyData {
MyData(GLfloat d1, GLfloat d2, GLfloat d3, GLfloat d4){ d[0] = d1; d[1]=d2; d[2]=d3; d[3]=d4;}
GLfloat d[4];
GLfloat & operator[](int i){ return d[i]; }
}

std::vector<MyData> pos;
pos.push_back(MyData(0.0, 1.0, 2.0, 3.0));

mickey
28th April 2006, 11:08
thanks, the first example compile, but I'm trying to understand better the thing trying const_cast....but don't compile this...


GLfloat* p = const_cast <GLfloat[4]> (pos);
//const_cast <GLfloat[4]> (pos); //has it sense?
position.push_back(p); //this don't compile

wysota
28th April 2006, 11:49
const_cast may not work here or may cause segfaults later on, as you create those arrays on stack and when you push them onto the vector, you'll only push the pointer to the array, which will become invalid when the array goes out of scope. So you should stick with creating those items on heap like in my first example.