Initializing struct vector with brace-enclosed initializer list

 ̄綄美尐妖づ 提交于 2019-12-04 03:29:45

A set of {} is missing:

std::vector<Vertex> data =
{ // for the vector
    { // for a Vertex
        {0.0f, 0.0f, 0.0f},      // for array 'position'
        {0.0f, 0.0f, 0.0f, 0.0f} // for array 'color'
    },
    {
        {0.0f, 0.0f, 0.0f},
        {0.0f, 0.0f, 0.0f, 0.0f}
    }
};

you need one more {} actually

vector<Vertex> data = {{{0.0f, 0.0f, 0.0f}, {0.0f, 0.0f, 0.0f, 0.0f}}};

one '{' for vector, one for struct, one (couple of) for struct member-arrays...

An object with vector members can also be initialized.

#include <iostream>
#include <string>
#include <vector>
using namespace std;


class Test
{
   public:
   struct NumStr
   {
      int num;
      string str;
   };

   Test(vector<int> v1,vector<NumStr> v2) : _v1(v1),_v2(v2) {}
   vector<int> _v1;
   vector<NumStr> _v2;
};

int main()
{
   Test t={ {1,2,3}, {{1,"one"}, {2,"two"}, {3,"three"}} };
   cout << t._v1[1] << " " << t._v2[1].num << " " << t._v2[1].str << endl;
   return 0;
}

2 2 two

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!