Trying to understand how to use the auto keyword correctly

May 22, 2013 at 6:08am
Guys/Gals, i am a definite n00b. i started trying to learn C++ as my first language about 3 days ago. Please be gentle with me lol. Having said that, I have a book that tries to explain how to use the auto keyword but the code in the lesson fails to compile both in Linux (using GCC compiler and in MS Visual C++ Studio.) Searching for an answer has brought me here. The decltype function is what i think i am missing. i will look that up but i want to be pointed in the right direction. the code in the lesson is...

//Pre-Processor Directive
#include <iostream>

//Declaring namespace type
using namespace std;

int main ( )
{
auto Flag = true;
auto Number = 2500000000000;

cout << "Flag = " << Flag;
cout << ", sizeof(Flag) = " << sizeof(Flag) << endl;
cout << "Number = " << Number;
cout << " , sizeof(Number) = " << sizeof(Number) << endl;

return 0;
}

and the fail message i get is ..

moses@linux-8sfs:~/C++_Lessons/cpp> g++ -o online3.5 online3.5.cpp
online3.5.cpp: In function ‘int main()’:
online3.5.cpp:6:10: error: ‘Flag’ does not name a type
online3.5.cpp:7:10: error: ‘Number’ does not name a type
online3.5.cpp:9:26: error: ‘Flag’ was not declared in this scope
online3.5.cpp:11:28: error: ‘Number’ was not declared in this scope


please ignore the //comments. they are mine. It seems the compiler is looking for a type declaration (or i have done something that makes my auto irrelevant). if anyone could just point me in the right direction, i will do the work. thanks in advance -- the Diesel

@giblit
Do you have c++11 turned on? Check your compiler settings.
yes i will check that . i thought that was automatic on the MS Studio version though. i will let you know

btw why are you trying to.get the size of a boolean
it's just an excercise in a teach-yourself book. i try not to skip ahead but my guess is the author is trying to teach memory management as well as variables and constants.


Last edited on May 22, 2013 at 6:13am
May 22, 2013 at 6:40am
Your code is valid.
From the error I can dedude that compiler thinks of auto in this case as of storage duration specifier, so you really does not have C++11 mode turned on.
May 22, 2013 at 6:42am
i thought that was automatic on the MS Studio version though. i will let you know
moses@linux-8sfs:~/C++_Lessons/cpp> g++ -o online3.5 online3.5.cpp


Say what?
Last edited on May 22, 2013 at 6:44am
May 22, 2013 at 6:56am
@cire
Shhhh... Ignorance is bliss.
May 22, 2013 at 11:30am
# g++ -o online3.5 online3.5.cpp

g++ (GCC), even the latest version, does default to the older standard. Add "-std=c++11" option and preferably "-Wall" too.
man g++


The C++ compiler of MS Visual Studio on the other hand does default to most of what it can. However, only the latest VS 2012 supports significant amount of C++11. Speaking of MS defaults, the default at least in Express is to still compile 32-bit binaries (but luckily the compiler can produce 64-bit code too.)
Last edited on May 22, 2013 at 11:31am
May 22, 2013 at 12:32pm
@MiiNiPaa thanks for the confirmation!!!!

@cire lol. i think you are laughing at the fact that i am pasting output from a GCC compiler and saying that it errored out in VS studio. i was just trying to show the error (as it was essentially the same on both compilers.) If not, i will read this again in a month or so and realize that there is only a very thin line between n00b-ness and idiocy (sometimes i cross over it).

@keskiverto thank you i will read the man pages and add those options. i appreciate the help.
May 22, 2013 at 12:45pm
@keskiverto. that was it. thank you!!!!
Topic archived. No new replies allowed.