How do I properly and easily initialize an instance of a class that contains a std::vector of some other class that in itself contains some data.
I understand that it is really hard to explain it in words, so I will instead write a piece of code that does not work, but it captures my intention.
#include <vector>
struct Point
{
float x, y;
};
struct Triangle
{
Point points[3];
};
struct Geometry
{
std::vector<Triangle> triangles;
};
int main()
{
Geometry instance
{
{{0,0}, {6, 0}, {3, 3}},
{{5,2}, {6, 6}, {7, 3}}
};
return 0;
}
This code does not work. Clang returns an error -
excess elements in struct initializer
I can not figure out why it is giving me this error.
I imagine that I can initialize
- the
std::vectorofTriangles, - then the array of
Points, - then the two
floats within eachPointobject.
How would I go about properly initializing an instance of Geometry class with some values without writing too much code using the initializer brackets?
If you have alternatives, then I am open to considering them.