Problems with auto and decltype

Hello,

I'm a complete beginner in programming itself. Being only learning the basics of Python and R for the last couple months and decided to also learn a bit of C++ programming.

I started out with the Tutorial in this same site but I got stuck in the auto/decltype part.

I'm using Ubuntu 12.04 and kate for writing the source code.

This is the code:

1
2
3
4
5
6
7
8
9
10
11

// random program
#include <iostream>
using namespace std;

int main()
{
  int zero = 0;
  auto nothing = zero;
}


First I tried compiling it with g++:

1
2
3
g++ random.cpp -o random
random.cpp: In function ‘int main()’:
random.cpp:8:8: error: ‘nothing’ does not name a type


After some research on these forums:

1
2
g++ -std=c++11 random.cpp -o random
cc1plus: error: unrecognized command line option ‘-std=c++11’


And lastly I tried something I saw in this post(http://www.cplusplus.com/forum/unices/116129/) but again it failed:

1
2
3
4
5
g++-4.8 random.cpp -o random
random.cpp: In function ‘int main()’:
random.cpp:8:8: error: ‘nothing’ does not name a type
   auto nothing = zero;
        ^


I'm out of ideas. I'm sure there is a simple solution for this problem but I'm rookie at both C++/programming in general (I'm a biochemistry student), as well as an Ubuntu user (I feel it to be very powerfull but I'm still not completly comfortable with the OS).

Sorry for the long post and hope it doesn't break in rule of the forums. Thanks anyway.
auto is a storage class specification, like static, register, or extern. It isn't a type, so you can't declare something of type auto. You can use it on any type, like so: auto int nothing;. It really isn't necessary to use it though, because it is assumed by default that all variables have automatic storage duration unless specified otherwise.
Last edited on
I'm following the tutorial on this very same page and it says that it is a type specifer. Indeed I've got this same problem when using decltype.

I have also had problems using the string class specifier (does that make proper sense?)

I'm just following the instructions here: http://www.cplusplus.com/doc/tutorial/variables/

I think the problem is that I'm using an out-dated compiler but I have no clue on how to use the C++11 version since the GNU compiler does not accept the std argument.
You're absolutely right that it is your compiler is outdated. I'm not too familiar with C++11 myself, as I still use a slightly older version of gcc/g++ at work.

Personally, I think automatic type deduction isn't necessary, and you really don't want to use it unless you *have* to. If the compiler has to automatically deduce the type, you have to do it as well every time you read your code, which can get confusing if you have whole bunch of variables with different types!

If you want to stay on the bleeding edge you can always download (I'm sure there's an apt-get repo that has it) the latest gcc for your distro. If you want to know what C++0x/C++11 features are supported by which versions of gcc, you can refer to this handy table: http://gcc.gnu.org/projects/cxx0x.html

C++ string is part of the standard library, so you either have to have a using namespace std directive in the file where you use it, or scope it properly (i.e. std::string). I prefer the latter, because then you're avoiding conflicts with all the other std library declarations and your own code.
I was just trying to use:

 
g++ -std=c++0x something.cpp -o something


And it happened to compile it with no problems at all. I don't understand why that works and the "-std=c++11" doesn't but it is enough for me now as it allows me to keep going on my learning.

Thank you very much.
Topic archived. No new replies allowed.