Help with initializing vectors

I understand that in earlier versions of C++ that you cannot initialize vectors in this fashion: std::vector<int> vec = { 1, 2, 3 };

It is also to my understanding that C++ 14 can handle this type of inititliazation.

I think where my problems lies is that I'm not requesting to use C++14 in the proper way inside of my CMAKE file. This is how I'm currently telling my CMAKE file to use C++14: set(cmake_CXX_FLAGS -std=c++14) I was wondering if this is the correct way to do it because I'm still getting an error when I try to initialize my vector with values.

Thanks for any help!

There is no = in the syntax. See http://www.informit.com/articles/article.aspx?p=1852519

What is the exact error message?
This is the error that I get when I use the = :

error: non-aggregate type
'std::vector<int>' cannot be initialized with an initializer list
std::vector<int> vec = { 1, 2, 3 };

This is the error that I get when I don't use the = :

error: expected ';' at end of
declaration
std::vector<int> vec { 1, 2, 3 };

Thanks again for your help!
The http://cpp.sh gives same error both with and without = in C++98 mode:
 In function 'int main()':
3:41: error: in C++98 'bar' must be initialized by constructor, not by '{...}'
3:41: error: could not convert '{10, 20, 30, 40, 50}' from '<brace-enclosed initializer list>' to 'std::vector<int>'


Thus, the cmake project config file. The following has some examples:
http://stackoverflow.com/questions/11783932/how-to-add-linker-or-compile-flag-in-cmake-file


What is the version of your compiler?
Last edited on
Topic archived. No new replies allowed.