6

Newbie here. I just got an error running from the terminal a C++ program I wrote: error: ‘stoi’ is not a member of ‘std’. They told me the compiler is too old.

I'm using Ubuntu 14.04.

My g++ version is 4.8.4.

How do I upgrade?

Pigna
  • 227

1 Answers1

5

You don't need to upgrade. Specify the standards version to g++. For example, to compile an example program from cppreference.com:

$ g++ --version
g++ (Ubuntu 4.8.5-2ubuntu1~14.04.1) 4.8.5
Copyright (C) 2015 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

$ g++ --std=c++11 -o test test.cpp
$ ./test
std::stoi("45") is 45
std::stoi("3.14159") is 3
std::stoi("31337 with words") is 31337
muru
  • 197,895
  • 55
  • 485
  • 740
  • 1
    so every time I use g++ I need to add --std=c++11? Can't I make C++11 the default standard? – Pigna Feb 09 '16 at 19:30
  • 2
    @Pigna If you run g++ that often, you should be writing Makefiles. – muru Feb 09 '16 at 19:32
  • @muru: If your machine pre-dates the standard then an update is required. Example: c++17/c++1z just released a few months ago, and my machine does not support the standard because its a few years old. Your answer is a solution to the OP's particular use case, not the posed question – charlestoncrabb Jun 17 '17 at 17:24
  • 1
    @charlestoncrabb then you'd be looking for https://askubuntu.com/questions/466651/how-do-i-use-the-latest-gcc-on-ubuntu. I don't really care about the posed question, because it's often the case that people think they want X when they want to solve Y. – muru Jun 17 '17 at 17:29
  • @muru What if you are compiling someone else's code and can't edit the MakeFiles. – mjwrazor Jun 04 '18 at 21:47
  • That's why well-written Makefiles support CC, CXX, CXXFLAGS and the like. – muru Jun 05 '18 at 00:01