6

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.

IAmInPLS
  • 4,051
  • 4
  • 24
  • 57
WewLad
  • 717
  • 2
  • 11
  • 22
  • What's your target compiler? VS2013? – R Sahu Apr 01 '16 at 01:19
  • 1
    Possible duplicate of [What is the easiest way to initialize a std::vector with hardcoded elements?](http://stackoverflow.com/questions/2236197/what-is-the-easiest-way-to-initialize-a-stdvector-with-hardcoded-elements) – Barmar Apr 01 '16 at 01:19
  • @Barmar not a duplicate of that; OP code is correct but the problem is how to configure the compiler for C++11 mode – M.M Apr 01 '16 at 02:21
  • @M.M But the answers there show other ways to initialize a vector that will work with older C++ versions. – Barmar Apr 01 '16 at 02:22
  • @R Sahu I'm not sure, I'm a bit of a beginner. I just use QT Creator and press run? – WewLad Apr 01 '16 at 10:16

1 Answers1

4

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]) );
Matt Messersmith
  • 12,939
  • 6
  • 51
  • 52