mardi 18 janvier 2022

How to compile using C++11 in VS Code

I am trying to use VS Code on Mac to run C++ code. I've used XCode before with no issue but am running into errors while trying to run this code. I am using the Microsoft C++ and CodeRunner extensions

#include <iostream>
#include <vector> 
#include <string>

using namespace std; 

int main () {
vector<string> names = {"Dan", "Sam", "Man"};  
vector<string> *name_ptr = &names;

cout << (*name_ptr).at(0);
 return 0;
}

I used Command+Shift+P to edit the Microsoft C/C++ Extension configurations and changed the C++ standard to "C+11." Doing this removed the error squiggles from the vector list but when I ran the code I still received the error:

"error: non-aggregate type 'vectorstd::__1::string' (aka 'vector<basic_string >') cannot be initialized with an initializer list"

I've tried using the advice from Error: non-aggregate type 'vector<int>' cannot be initialized with an initializer list but I am unsure how to pass the "g++ -std=c++11 myfile.cpp" argument to the compiler. I've tried putting that argument under compiler arguments in the C/C++ Extension configurations settings to no avail. I am not too familiar with how compilers so I am stuck here.

Aucun commentaire:

Enregistrer un commentaire