1. ## Vector of structures

Hello, I'm currently writing a model loader, and I have quite a few structures for different things, vertices, normals, materials, that stuff. Anyways, in my main model class, I want to have vectors to hold many of those structures. I don't quite know how to explain it, but here's what I mean in code:

Code:
```typedef struct
{
USHORT flag;
USHORT vertex_index1, vertex_index2, vertex_index3;
USHORT normal_index1, normal_index2, normal_index3;
USHORT smothing_group;
} TRIANGLE;

class Model
{
public:
private:
vector<TRIANGLE> triangles;
}```
As you can see, I can access all the triangles in the model from that one vector. I like that idea, because I can allocate the space as needed, and it's more encapsulated like that.

Anyways, on to my problem. How would I initialize these, add another triangle, etc? Is this even possible?

2. Of course it's possible. It is also eased by the addition of C++ struct constructors (making structs nearly (?) identical to classes). Here's a quick example working with your class. Maybe some ideas. Maybe some very very messy code too

Code:
```#include <vector>
#include <iostream>
typedef unsigned short USHORT;
typedef struct Triangle
{
USHORT flag;
USHORT something_group;
Triangle()
{
flag = 0;
something_group = 0;
}
Triangle(int a, int b)
{
flag = a;
something_group = b;
}
} TRIANGLE;

class Model
{
public:
triangles.push_back(Triangle());
}
void AddTriangle(int flag, int something_group) {
triangles.push_back(Triangle(flag, something_group));
}
TRIANGLE GetTriangle(int i) {
return triangles[i];
}
private:
std::vector<TRIANGLE> triangles;
};

int main(int argc, char *argv[])
{
Model model;
std::cout << model.GetTriangle(1).flag;
return 0;
}```

3. Yes, I tried what you said, and I still get an error when trying to add another struct to the vector.

Code:
`triangles.push_back(Triangle());`
I got an error because function hasn't been delcared, supposidly.

4. Originally Posted by Highland Laddie
Yes, I tried what you said, and I still get an error when trying to add another struct to the vector.

Code:
`triangles.push_back(Triangle());`
I got an error because function hasn't been delcared, supposidly.

Well what is Triangle? (Case does matter!) If you say

Code:
```TRIANGLE trianglestruct;
triangles.push_back(trianglestruct);```
That should work fine. It looks to me like you might be a bit confused... it looks like you are trying to call a function or perhaps make a new triangle(??) of type TRIANGLE. You can't really define a function in a struct, and also, if you are trying to make a "new" TRIANGLE, you would want to add another variable, or use the 'new' operator.

5. Uhh, yeah, it should. I found the problem. It was me. I made a stupid mistake, I already forget what it was, but it was stupid. It's working now, by the way. :P

Thanks a bunch!

6. This compiles VC++ 6.0 (not an authority for C++ standards by any means!) but also on the latest version of Dev-C++ 4.9.9 (which uses g++, somewhat more trustworthy?). I am not all that sure if this truely makes it valid, but it seems logical. I call the class constructor to return a new class object to throw in the vector. If instantiating the object beforehand is necessary to do this, then whatev, but it does seem at least logical if not syntactically correct.

7. Unless you need part of your code to work in C, you can just change
Code:
```typedef struct Triangle
{
// ...
} TRIANGLE;```
to
Code:
```struct Triangle
{
}```
and use Triangle everywhere to avoid confusion between Triangle and TRIANGLE. The typdef is only helpful in C, not C++.