1

I can't compile the following code

auto test = 42.02;
double right = std::stod(stck.top());

I'm using Code::Blocks and activated the build option to follow c++11 standard. The compiler does not complain about the auto declaration and compiles, when I put the line below in comments.

I included the string header. I'm not using a namespace.

I have no idea why this does not compile. Please help me! edits: My compiler is Standard MinGW GCC 4.9

For simplicity reasons, I tried the following: compiled with -std=c++11

#include <string>

int main(){
     double pi = std::stod("3.14");
     return 0;
}

I get the following error:

error: stod is not a member of std.

infinitezero
  • 1,610
  • 3
  • 14
  • 29
  • 3
    What is `stck`? Please provide a [mcve]. – 5gon12eder Mar 01 '16 at 00:33
  • 1
    Maybe your compiler standard library isn't updated for C++11? – nneonneo Mar 01 '16 at 00:33
  • 1
    CodeBlocks is **not** a compiler. Are you using MinGW? IIRC, versions from a year or two ago had `stod`, `stoi`, `to_string` etc. not defined even if you enabled `-std=c++11` – Praetorian Mar 01 '16 at 00:34
  • I'll look it up. stck is an instance of a stack but that's not relevant to this example. – infinitezero Mar 01 '16 at 00:38
  • I'm using Standard MinGW 32-bit Edition GCC 4.9 Series. – infinitezero Mar 01 '16 at 00:41
  • Please include a declaration of `stck` and the compiler error – Tas Mar 01 '16 at 00:50
  • The compiler error was already stated in the headline as well. I edited it and also posted a more simplified version of the problem. – infinitezero Mar 01 '16 at 00:55
  • 4
    (http://stackoverflow.com/questions/16132176/problems-with-stdstoi-not-working-on-mingw-gcc-4-7-2) has relevant information about lack of support for these functions in **MinGW** ports of earlier g++. But first, have you remembered to specify C++11 or later, e.g. via `-std=c++11`? – Cheers and hth. - Alf Mar 01 '16 at 01:01
  • Please list all flags passed to the compiler to build that code. Ideally manuallly run the compiler with those flags on that code to confirm those flags are indeed ones that repo the error. – Yakk - Adam Nevraumont Mar 01 '16 at 01:52
  • I only use-std=c++11. Also, I can get other c++11 specific code (like auto) to compile. – infinitezero Mar 01 '16 at 02:14
  • This is the same old issue that the dead version of mingw does not support `stoi`, `stod` etc. The best fix is to install MinGW-w64 as the compiler, which does support those functions (and c++11 threads) – M.M Mar 01 '16 at 13:23

2 Answers2

1

std::stod is only available if you are at least using std=c++11 to compile. Therefore, when you compile, just add the flag -std=c++11 and you will be able to use stod

NickLamp
  • 862
  • 5
  • 10
0

Seems like you've most likely misspelled std::strtod()

You'll also need to

#include <cstdlib>
Mark Bessey
  • 19,598
  • 4
  • 47
  • 69
  • seems not http://www.cplusplus.com/reference/string/stod/ – pm100 Mar 01 '16 at 00:46
  • Oh, good point. In that case, probably the compiler's not in c++11 mode, in which case using the older standard function will still work :-) – Mark Bessey Mar 01 '16 at 00:47
  • 2
    **−1** "Seems like you've most likely misspelled std::strtod", no. Also, noting in passing that where you really need `strtod`, including `` is less troublesome than ``. But this latter point is a bit subjective: some folks place a lot of weight on appearing to be C++-ish, and argue that `` is the C++ way (it's silly, but they do). – Cheers and hth. - Alf Mar 01 '16 at 01:02