WewLad
WewLad

Reputation: 767

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

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.

Upvotes: 6

Views: 9691

Answers (1)

Matt Messersmith
Matt Messersmith

Reputation: 13747

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]) );

Upvotes: 4

Related Questions