C++ Array Problem

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

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 on other sites
Do you ever actually allocate space for the arrays anywhere?

Share on other sites
Quote:
 Original post by Super Llamaboth used in a variable-length array as follows:

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

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 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 on other sites
Quote:
 Original post by Super Llamahow do you access it once its declared? Vertices[3]?

Almost. Corrected above.

Share on other sites
Do you need to include any headers or namespaces first?

Share on other sites
#include <vector>

1. 1
2. 2
Rutin
21
3. 3
4. 4
frob
17
5. 5

• 9
• 12
• 9
• 33
• 13
• Forum Statistics

• Total Topics
632595
• Total Posts
3007266

×