可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
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.