0

Im trying to compile this code via g++,

#include <iostream>
#include <set>
using namespace std;

std::set<int> s;

int main(){
for(int i=0;i<10;i++) s.insert(i);

for(auto target: s){
cout<<target<<endl;
}
return 0;
}

but g++ 4.8 gives me this error:

ali@Melkor ~/Desktop/Project $ g++ a.cpp
a.cpp: In function ‘int main()’:
a.cpp:10:10: error: ‘target’ does not name a type
 for(auto target: s){
          ^
a.cpp:14:1: error: expected ‘;’ before ‘return’
 return 0;
 ^
a.cpp:14:1: error: expected primary-expression before ‘return’
a.cpp:14:1: error: expected ‘;’ before ‘return’
a.cpp:14:1: error: expected primary-expression before ‘return’
a.cpp:14:1: error: expected ‘)’ before ‘return’
ali@Melkor ~/Desktop/Project $ 

Is there anything wrong with my code?

1 Answer 1

3

There is nothing wrong with your code, but the auto specifier is present since c++11 version of the standard of C++ programming language.

You must use

g++ -std=c++11 a.cpp

to compile your code.

Not the answer you're looking for? Browse other questions tagged .