Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Compiler error: "Non-aggregates cannot be initialized with initializer list."

Tags:

c++

qt

When trying to create a simple vector in C++, I get the following error :

Non-aggregates cannot be initialized with initializer list.

The code I'm using is:

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

I tried to put:

CONFIG += c++11 

into my .pro file, saved and rebuilt it. However, I still get the same error. I'm using what I assume to be Qt 5.5, here's what happens when I press About if it means anything to you: Qt's About.

Any help is appreciated.

like image 985
WewLad Avatar asked Apr 01 '16 00:04

WewLad


1 Answers

The following line:

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

won't compile pre C++11.

However, you could do something like this:

static const int arr[] = {1, 2, 3, 4, 5};
vector<int> theVector (arr, arr + sizeof(arr) / sizeof(arr[0]) );
like image 64
Matt Messersmith Avatar answered Oct 04 '22 20:10

Matt Messersmith