0

I have received the following error for the following code on Visual Studio Code for Mac.

ArrayStudy.cpp:21:19: error: cannot deduce type of initializer list because std::initializer_list was not found; include <initializer_list>
    for (auto x : {10,21,32,43,54,65})

I included #include <initializer_list> after noting it was absent, but continued to receive the error. I'm using Apple clang version 13.0.0 (clang-1300.0.29.30).

At this point I am stumped. Can someone help me get this code running with a short explanation of what I missed?

#include <iostream>
#include <string>
#include <fstream>
#include <initializer_list>

void print();

int main(){

    print();

}

void print() {
    int v[] = {0,1,2,3,4,5,6,7,8,9};

    for (auto x : v)
        cout << x << '\n';

    for (auto x : {10,21,32,43,54,65})
        cout << x << '\n';
}
cigien
  • 57,834
  • 11
  • 73
  • 112
  • 3
    Which compiler and compiler version are you using and with what flags are you invoking the compiler? "_and continued to receive the error._": Make sure you actually compiled the modified file and didn't accidentally compile an old version e.g. because you didn't save the file. – user17732522 Feb 19 '22 at 03:42
  • Why is your `using` statement in the middle of your headers, instead of after them? For that matter, [why are you using that `using` statement in the first place](https://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice)? – Remy Lebeau Feb 19 '22 at 03:54
  • Apple clang version 13.0.0 (clang-1300.0.29.30). I did not compile an old version of the file. I removed the using statement as well. – Jeffrey Semon Feb 19 '22 at 04:19
  • What options are you passing to the compiler? Use something like `-std=c++20` or similar, but at least `-std=c++11`. This is a C++11 feature. – user17732522 Feb 19 '22 at 04:49
  • Please add all relevant information as an [edit] to the question, instead of as comments. I've added the compiler details from your comment, but please add the compiler flags that you're using, as has already been requested. – cigien Feb 19 '22 at 15:15
  • How do you run the -std=c++11 command on the Visual Studio code terminal? Is the terminal where this command is run? How ill this help me with the question above? – Jeffrey Semon Feb 22 '22 at 01:53
  • I also wondered if there is an error in the second for statement "where every element of the array places a copy in x". Is that the correct logic for this line of code? – Jeffrey Semon Feb 22 '22 at 03:39

0 Answers0