简体   繁体   English

如何初始化静态const向量

[英]How to initialize static const vector

I have a member in my class defined like this inside my header file: 我的头文件中的类中定义了一个成员:

static const vector<note_name> noteMap;

In my source file I want to assign values to the vector so I tried: 在我的源文件中,我想为向量分配值,所以尝试了:

const vector<note_name> AppSettings::noteMap = {...}

But I'm getting an error: Non-aggregate type 'const vector<note_name>' cannot be initialized with an initializer list . 但是我遇到一个错误: Non-aggregate type 'const vector<note_name>' cannot be initialized with an initializer list Any ideas how I could initialize this vector? 有什么想法可以初始化这个向量吗? Using an array is not an option btw. 顺便说一句,使用数组不是一种选择。

Write a function that returns a vector<note_name> with the appropriate values filled in, and use that function to initialize noteMap . 编写一个函数,返回一个带有填充了适当值的vector<note_name> ,并使用该函数初始化noteMap That will always work; 那将一直有效; with more information about the initializers it might be possible to come up with a better solution. 有了有关初始化程序的更多信息,就有可能提出更好的解决方案。 Or, if you have C++11, you can use an initializer list as in the example. 或者,如果您具有C ++ 11,则可以使用示例中的初始化列表。

Or, with TR1 or Boost or C++11, change the vector to an array<note_name> . 或者,使用TR1或Boost或C ++ 11,将vector更改为array<note_name> The array template supports aggregate initialization. array模板支持聚合初始化。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM