• Advertisement
Sign in to follow this  

C++ Array Problem

This topic is 3398 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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.

Share this post


Link to post
Share on other sites
Advertisement
Quote:
Original post by Super Llama
both used in a variable-length array as follows:

There are no VLAs in C++. Use vectors.

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
std::vector<Vert> Vertices;

Usage:
Vertices.push_back(CreateVertex(100,50,10));
Once there is a first element, Vertices[0] will access it.

Share this post


Link to post
Share on other sites
Quote:
Original post by Super Llama
how do you access it once its declared? Vertices[3]?

Almost. Corrected above.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement