Getting Semantic error declaring array length

I get his error message: error: Semantic Issue: Variable length array of non-POD element type 'ofVec2f’
in ofVec2f positions[numParticles];

  
void testApp::renderTexturedMesh() {  
    int numParticles = particles.size();  
    ofVec2f positions[numParticles];  
    for ( int i = 0; i < particles.size(); i++ ) {  
        positions[i] = particles[i].getPosition();  
    }  
      
    ofSetHexColor( 0xffffff );  
    if ( isImageSet ) skinTexture.bind();  
    vboMesh.updateVertexData( positions, numParticles );  
    vboMesh.drawElements( GL_TRIANGLES, mesh.getNumIndices() );  
    if ( isImageSet ) skinTexture.unbind();  
}  

Any ideas how to fix that?

After changing the Compiler in the Build options to LLVM GCC4.2 the program compiles but the error message still there

variable length arrays are only available through compiler extensions (e.g. in gcc), they are not part of the C++ standard afaik.
you should consider using std::vector instead.
http://stackoverflow.com/questions/3782628/why-does-c-not-support-variable-length-arrays
http://stackoverflow.com/questions/5730101/when-are-variable-length-arrays-legal

exactly – static arrays have their memory allocated at run time and therefore can not be variable length. this can be confusing if you’re coming from java, where arrays and ArrayList/Vector are used dynamically.

Cool thanks guys, so if I need to use a std::vector instead how can I feed my positions[] to ofVbo? because ofVbo.updateVertexData() requires an array, any ideas?

updateVertexData() doesn’t necessarily require an array, it requires a pointer to a bunch of vertices. they could be stored in an array, or in a vector. if you want the pointer to the beginning of the vector, one shorthand way of doing this is saying “&vector[0]” or “&(vector[0])”. that means “the address (&) of the vector’s (vector) first item ([0])”. so:

  
  
vbo.updateVertexData(&myPoints[0], myPoints.size());  
  

in general i would recommend you use ofVboMesh instead. it uses ofMesh functions, but internally is loaded into a VBO. it’s way easier.

@kyle: i wanted to recommend that, too, but was not sure if the size of one element in guaranteed to be the same in an array and a vector. that could mess up pointer arithmetic, isn’t it? do you know?

yes – stl vector is implemented with an array, so the data is guaranteed to be packed/contiguous. this isn’t true for all stl datatypes, but it’s true for vector. http://stackoverflow.com/questions/247738/is-it-safe-to-assume-that-stl-vector-storage-is-always-contiguous/247764#247764

this is one of the reasons i feel comfortable using vectors so often :slight_smile: it’s a very crucial feature that you can say &vec[0].

btw, most of the times you don’t need to use ofVbo directly, but ofVboMesh, the api is simpler than the vbo and you don’t need special collections to store your vertexes, tex coords… as everything is contained in the mesh

Good stuff thanks a lot guys!