编译器错误:“不能用初始化列表初始化非聚合。”

Wew*_*Lad 6 c++ qt

尝试在 C++ 中创建一个简单的向量时,出现以下错误:

非聚合不能用初始化列表初始化。

我正在使用的代码是:

#include <iostream>
#include <string>
#include <vector>

using namespace std;

int main(int argc, char *argv[])
{
    vector <int> theVector = {1, 2, 3, 4, 5};
    cout << theVector[0];
}
Run Code Online (Sandbox Code Playgroud)

我试着把:

CONFIG += c++11 
Run Code Online (Sandbox Code Playgroud)

进入我的.pro文件,保存并重建它。但是,我仍然遇到相同的错误。我正在使用我认为是 Qt 5.5 的东西,About如果它对您有意义,那么当我按下时会发生以下情况:Qt's About

任何帮助表示赞赏。

Mat*_*ith 4

以下行:

vector <int> theVector = {1, 2, 3, 4, 5};
Run Code Online (Sandbox Code Playgroud)

不会编译 C++11 之前的版本。

但是,你可以这样做:

static const int arr[] = {1, 2, 3, 4, 5};
vector<int> theVector (arr, arr + sizeof(arr) / sizeof(arr[0]) );
Run Code Online (Sandbox Code Playgroud)