Error: Assigning to an array from an initializer list

匿名 (未验证) 提交于 2019-12-03 01:57:01

问题:

I have a class such as:

class dialog {     public:     double dReturnType[][5][3]; }; 

#include  #include  include   using namespace std; #include "dialog.h";  int main(int argc, char *argv[]) {     dialog People;     People.dReturnType[0][1] = {1.2,2.3,6.6};     return 0; } 

It returns:

[Warning] extended initializer lists only available with -std=c++11 or -std=gnu11 [enabled by default] [Error]: assigning to an array from an initializer list

I've looked it up online a bit and really couldn't find a way to get around this. I'd prefer not editing the class within it's on class file (kinda defeats the purpose). Any help?

Note: the class is in a separate project file

回答1:

Initializer lists are just usable during initialization.

If you want use std::initializer_list after initialization:

auto init = std::initializer_list({1.2,2.3,6.6}); std::copy(init.begin(), init.end(), your_array); 


回答2:

You can't initialize a extended list unless you're on c++11.

And If I was you a good habit is to use * instead of empty "[]" and allocate memory when you know the size (with new or malloc). dReturn type on your program is a pointer of matices.

And you're giving a full list to only one member of the vector.

People.dReturnType[0]={1.2,2.3,6.6}; 

That makes more sense.

Try to encapsulate and use/create initializer functions that will help you to do that too. C++ will put all 0 at start, but you can do a function and call:

dialog People("the_atributes_are_here"). 

It's a good pratice to make the dReturnType private and use functions to acess it data and insert/modify things. But that is up to you.



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