auto

Following the tutorials for auto;
why this code can't compile?
error: 'bar' does not name a type
1
2
3
4
5
6
7
8
9
#include <iostream>

using namespace std;

int main(){
	int foo = 0;
	auto bar = foo;  // the same as: int bar = foo; 

}
Last edited on
Did you put

1
2
3
4
5
6
7
8
#include<iostream>
using namespace std;

int main()
{
  int foo = 0;
  auto bar = foo;
}
Are your compiler recent enough and have you enabled features for C++11 (or later)?
Last edited on
I'm using gcc 4.8.1. How can I check this feature is enabled?
Use command line flag: -std=c++11

See 'man gcc'
Is this the command "flag: -std=c++11" ?

got an error that can't recognize!
Do you use IDE or launch compiler from command line.
Proper command line looks like this:
g++ -std=c++11 -O2 -Wall -pedantic main.cpp
I'm using IDE but I tried with "g++ -std=c++11 -O2 -Wall -pedantic"
on command line still following error:

warning: unused variable 'bar' [-Wunused-variable]

still following error:
warning
It is not an error. It is a warning saying that mere existence of varable bar is useless as you do not use it anywhere.
You right but still not complied! Should be compiled, Isn't it?
Last edited on
It should be compiled. Did you look for any new exe file in working directory?

I'm using IDE
Which IDE? It would be better to just set it to use C++11
Actually it's text editor(sublime). I deleted the related compiled file(.exe) to make

sure if the new one compiled.
Topic archived. No new replies allowed.