Problem with compiling vectors in terminal

I don’t quite understand the problem here. The codecademy platform compiles and executes the code by itself, so I wanted to check it out in the real terminal and stumbled across an error. The part of the code I have issues is: std::vector even = {2, 4, 6, 8, 10};
the terminal identifies this line as an error and says this:

non-aggregate type ‘std::vector’ cannot be initialized with an initializer list

std::vector even = {2, 4, 6, 8, 10};

Help, how can i solve this?

1 Like

You can try to add the vector datatype, like std::vector<“datatype”> name = {values};
But then you will probably encounter another error. Depends on your compiler.
“[Error] could not convert ‘{2, 4, 3, 6, 1, 9}’ from ‘’ to ‘std::vector’”
I got this and found out it was from my compiler because is a bug that has been fixed and my compiler is outdated. So, try this out.

Same issue here. Has anyone got a solution to this?

Can you link to the exercise?

Also, can you share both your code and the error message?

(There is a “Copy to Clipboard” button at the bottom of the exercises. To post the code in the forums, you can use the </> button (see: [How to] Format code in posts)

Hi,

the issue came up during the Review section of Vectors
https://www.codecademy.com/courses/learn-c-plus-plus/lessons/cpp-vectors/exercises/review

#include <iostream> #include <vector> int main() { std::vector<int> numbers = {2, 4, 3, 6, 1, 9}; int even=0; int uneven=0; for (int i = 0; i < numbers.size(); i++) { if (numbers[i]%2 == 0) { even = even + numbers[i]; } else { uneven = uneven + numbers[i]; } } std::cout << "Even numbers total: " << even << "\n"; std::cout << "Uneven numbers total: " << uneven << "\n"; }

The Code works fine in the Codecademy environment, but kept crashing in my VSCode. So I was googling for a while and came across a solution on stackoverflow to include -std=c++17.

Screenshot from VSCode Terminal (same code as above):

So after 2h of googling around, I sort of understood that the compiler version that my computer uses is below C++11 and that is why it can’t read the initializer list.

I can’t figure out how to update to a newer compiler version (Macbook Pro 2014 - Big Sur).

Does that make sense to you and do you have a solution?

Thanks,
Paul

1 Like

I am not familiar with your environment, but this post looks useful (if you do decide to edit your tasks.json file, make sure to copy the contents of the file before making any changes so that if things don’t go well, you can at least reverse the changes):

1 Like

Thanks for the effort, but I already tried that and it did not work.

The funny thing is that if I add --std=c++17 manually in the Terminal it will compile just fine.

Perhaps the suggestion before the “Old, outdated answer” can be explored.
(c++ - How can I set a default tasks.json file for Visual Studio Code? - Stack Overflow)

Someone familiar with VSC, C++ and macOS should be able to help you accomplish what you want to do.

1 Like