草庐IT

c++ - 编译器错误 : "Non-aggregates cannot be initialized with initializer list."

coder 2024-02-23 原文

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

Non-aggregates cannot be initialized with initializer list.

我使用的代码是:

#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];
}

我试着把:

CONFIG += c++11 

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

感谢任何帮助。

最佳答案

下面一行:

vector <int> theVector = {1, 2, 3, 4, 5};

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

但是,您可以这样做:

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

关于c++ - 编译器错误 : "Non-aggregates cannot be initialized with initializer list.",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36346292/

有关c++ - 编译器错误 : "Non-aggregates cannot be initialized with initializer list."的更多相关文章

随机推荐