C++ Array Problem

Started by
6 comments, last by ToohrVyk 15 years, 5 months ago
I'm trying to declare a reusable class for mesh storage. I have structs for Vertices and Triangles, both used in a variable-length array as follows:

class Mesh
{
public:
    int VertCount; int TriCount;
    Vert Vertices[];
    Tri Triangles[];
    LPSTR texpath;
};


However, whenever I write to one array, it resets the previous array. For example:

Vertices[0] = CreateVertex(100,50,10);
Vertices[1] = CreateVertex(50,100,30);
Vertices[2] = CreateVertex(10,50,30);
//if I draw the vertex array here, it will show up fine, but it won't be able to connect without the triangles.
Triangles[0] = CreateTriangle(0,1,2);
//now if I try to draw it, only Vertices[2] retains its original position, the other two are zeroed out.


Then I tried:

Vertices[0] = CreateVertex(100,50,10);
Vertices[1] = CreateVertex(50,100,30);
Vertices[2] = CreateVertex(10,50,30);
Triangles[3] = CreateTriangle(0,1,2);


and it all dispalyed fine except Vertices[0]. Vertices[0] was empty. This is a very very very wierd problem, I'm not very used to cpp arrays (this same code worked fine when ported to VB) and I really need some help! If there's a better way to have two non-fixed arrays in a class, that would be great, or if there's some obscure C++ eqivilant to ReDim, that would be even better.
It's a sofa! It's a camel! No! It's Super Llama!
Advertisement
Do you ever actually allocate space for the arrays anywhere?
Quote:Original post by Super Llama
both used in a variable-length array as follows:

There are no VLAs in C++. Use vectors.
How do you declare a vector? I'm using unmanaged C++.

EDIT: Nevermind, I googled it the second after I posted :P

so its like:

vector<Vert> Vertices;

how do you access it once its declared? Vertices<3>?

EDIT:

Lol I googled it again, same as arrays
It's a sofa! It's a camel! No! It's Super Llama!
std::vector<Vert> Vertices;

Usage:
Vertices.push_back(CreateVertex(100,50,10));
Once there is a first element, Vertices[0] will access it.
Quote:Original post by Super Llama
how do you access it once its declared? Vertices[3]?

Almost. Corrected above.
Do you need to include any headers or namespaces first?
It's a sofa! It's a camel! No! It's Super Llama!
#include <vector>

Read this, too.

This topic is closed to new replies.

Advertisement