c++ - Why does the following code work on online ide(gcc 7.2.0) but gives error on ubuntu? -


when run following code on ubuntu(gcc (ubuntu 5.4.0-6ubuntu1~16.04.4)):

#include<iostream> #include<vector> #include<list> using namespace std; int main(){ vector <int> v; v.push_back(1); v.push_back(2); v.push_back(3); v.push_back(4); v.push_back(5); list<int> temp; for(auto i:v){     cout<<i<<" ";     temp.push_back(i); } for(auto i:temp){     cout<<i<<" "; } } 


following errors:

try.cpp: in function ‘int main()’: try.cpp:13:10: error: ‘i’ not name type  for(auto i:v){           ^ try.cpp:17:1: error: expected ‘;’ before ‘for’  for(auto i:temp){  ^ try.cpp:17:1: error: expected primary-expression before ‘for’ try.cpp:17:1: error: expected ‘;’ before ‘for’ try.cpp:17:1: error: expected primary-expression before ‘for’ try.cpp:17:1: error: expected ‘)’ before ‘for’ try.cpp:17:10: error: ‘i’ not name type  for(auto i:temp){           ^ try.cpp:20:1: error: expected ‘;’ before ‘}’ token  }  ^ try.cpp:20:1: error: expected primary-expression before ‘}’ token try.cpp:20:1: error: expected ‘;’ before ‘}’ token try.cpp:20:1: error: expected primary-expression before ‘}’ token try.cpp:20:1: error: expected ‘)’ before ‘}’ token try.cpp:20:1: error: expected primary-expression before ‘}’ token 


when run code on online ide works fine.
what problem code?
link code on online ide:no errors


your code uses of c++11 features such range based loops , auto specifier don't compile c++11 standard. need enable c++11 support including -std=c++11 flag when compiling:

g++ -std=c++11 -o try try.cpp 

the online compiler has enabled using -std=gnu++1z flag.


Comments

Popular posts from this blog

Is there a better way to structure post methods in Class Based Views -

performance - Why is XCHG reg, reg a 3 micro-op instruction on modern Intel architectures? -

jquery - Responsive Navbar with Sub Navbar -