c++ - 无法初始化数组

标签 c++ compiler-errors

好吧,我是 C++ 的新手(从 VB 转移过来),我似乎无法弄清楚如何在不执行以下操作的情况下填充数组:

array[0] = 1
array[1] = 2 

等等等等

正如所料,我试过这个:

float posVector[3]; //Declared outside of int main() as needs to be global
posVector = {0.0f,0.0f,0.0f}; //Inside int main()

导致显示此错误的原因:

extended initializer lists only available with -std=c++0x or -std=gnu++0x [enabled by default]

到目前为止,我在网上找到的任何东西都没有任何帮助。因此,如果您能给我任何解决此问题的建议,我们将不胜感激!

最佳答案

一旦声明了数组,就不能使用初始化列表对其进行初始化。你可以去

float posVector[] = {0.0f,0.0f,0.0f};

或者更好的选择 std::vector :

#include <vector>

std::vector<float> posVector = {0.0f,0.0f,0.0f};

关于c++ - 无法初始化数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32853565/

相关文章:

c++ - C++ 上的函数重新声明

compiler-errors - 如何解决移位运算符语法错误?

dll - 静态链接搜索动态符号(VS 2015)

C++/CMake :Undefined reference to imwrite in OpenCV

c++ - 使用boost::bind将 map 作为参数传递

android - AOSP建筑: Make file shared libraries issue

c# - C#错误: The contextual keyword 'var' may only appear within a local variable declaration

c++ - 哪个是正确使用 C 中的头文件?

c++ - 附加到容器

c++ - 从 32 位数据类型中有效地删除 2 位