g++ 'stod' was not declared in this scope [duplicate] - c++

This question already has answers here:
cygwin g++ std::stoi "error: ‘stoi’ is not a member of ‘std
(5 answers)
Closed 6 years ago.
I am trying to compile the following code using g++ (version 4.8.1) in cygwin, and it seems that it cannot use the function stod():
//test.cpp
#include<iostream>
#include<vector>
#include<string>
using namespace std;
int main(){
string a="1.23";
cout<<stod(a);
return 0;
}
I keep getting this eroor:
test.cpp:9:14: error: 'stod' was not declared in this scope
cout<<stod(a);
I read another thread that has the same problem. There, people suggested to use c++11 to address it. So I tried both the following commands to compile it but still got the same error:
g++ -std=c++0x test.cpp -o test
g++ -std=c++11 test.cpp -o test
Does anyone know a solution to this problem?
And how do I know that c++11 is enabled? Do I need to modify my code in order to use it?
Thanks a lot!

It works in GCC 4.8 on Coliru (http://coliru.stacked-crooked.com/a/8a68ad0ca64c1bff) and also in Clang on my machine. It could be that somehow your Cygwin system doesn't support this function. I suggest you work around it by simply using good old strtod() instead. That's probably what stod() uses under the hood anyway.

Related

error: 'stoi' was not declared in this scope (Code::Blocks 16.01 on Windows 10) [duplicate]

This question already has answers here:
std::stoi doesn't exist in g++ 4.6.1 on MinGW
(4 answers)
Closed 6 years ago.
I get the error: 'stoi' was not declared in this scope.
#include <string>
using namespace std;
...
int x;
x = stoi(arg[0]);
I am running Code::Blocks 16.01 on Windows 10 with the -std=c++11 setting.
I didn't find any useful information on this page:
‘stoi’ was not declared in this scope
I saw somewhere that upgrading gcc can fix this problem, but I didn't find an appropriate installation on the page: https://mingw-w64.org/doku.php/download
The same code works fine in Code::Blocks 13.12 on Linux Mint 17.3.
Is there a recommended fix for this problem? Can this be fixed by using the 64 bit version of MinGW (assuming that is compatible with Code::Blocks 16.01)?
Update
there is a workaround, using atoi and c_str instead:
x = atoi(arg[0].c_str());
I believe that this is a bug with MinGW. For more information, check out this StackOverflow post. Specifically, DRH's answer.

std::stod is not a member of std [duplicate]

This question already has answers here:
Problems with std::stoi, not working on MinGW GCC 4.7.2
(2 answers)
Closed 6 years ago.
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.
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
Seems like you've most likely misspelled std::strtod()
You'll also need to
#include <cstdlib>

Error with stoi and debugged with gdb [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
My OS is ubuntu 14.04, laptop, i7.
The g++ version is g++ (Ubuntu 4.8.2-19ubuntu1) 4.8.2.
I tried to run a simple code to test stoi:
#include <string>
int main()
{
std::string s = "123";
int i = std::stoi(s);
}
When I compile it with: g++ -g prueba2.cpp, I get:
prueba2.cpp: In function ‘int main()’:
prueba2.cpp:6:12: error: ‘stoi’ is not a member of ‘std’
int i = std::stoi(s);
^
When I debug it twice first with g++ -std=c++0x -g prueba2.cpp (I also tried with -std=c++11) and then with dbg, I got:
Then, I also did a simple search and followed the suggestions made in here1, here2 and here3, and none worked.
Am I doing something silly?
Yeah, I think you're doing something pretty silly. You probably compiled the first code, which doesn't have the std::cout statement, and you probably executed the compilation steps without -std=c++11 which would result in std::stoi not being included beecause std::stoi is from C++11 and onward. The result is still the old executable which prints out nothing.
Recompile using -std=c++11 and make sure that you saved your file correctly. Your code clearly works.
Note: the vanilla port of GCC of MinGW on Windows is flawed and has a few bugs related to C++11 and onwards; using MinGW-w64, if you ever decide to compile on Windows, can help the problem.
std::stoi is a C++11 feature. Therefore your code only compiles, if you use the -std=c++11 flags (or the equivalent -std=c++0x flag that you mentioned, which has nothing to do with debugging).
The terminal session you provided also shows that compilation works with those flags and your program runs fine without any problem. If you want to print the parsed result, you can do it like that: std::cout << i << std::endl
If you don't want to use C++11 features, you can use the >> stream operator to parse your string to an int:
stringstream ss(s);
int i;
ss >> n;
But beware: Other than with stoi, you won't get an exception, if your input doesn't contain a valid number. You will have to check the stream's status yourself.

c++: compile with library argument in xcode [duplicate]

This question already has answers here:
xcode library not found
(14 answers)
Adding Linker Flags in Xcode
(3 answers)
Closed 8 years ago.
In terminal, this works:
g++ -lgmp main.cpp
while this does not:
g++ main.cpp
I am using X-code as my IDE and how could I tell X-code to add the statement '-lgmp' when compiling? Thank you:)
Edit: I did according to the article providing the answer:
but I still fail:
and this is the error message:
More information:
I have to use c++98 instead of c++11 because one of my package Gurobi requires that. Do you know any solution? thank you:)

std::to_string not available in header string [duplicate]

This question already has answers here:
to_string is not a member of std, says g++ (mingw)
(13 answers)
Closed 8 years ago.
I am trying to use std::to_string(), of course with #include <string>, but the compiler gives me the error that it was not declared in the scope, after lots of searching I have tried the fixes of going to compiler settings and have g++ follow... -std=c++11,
downloading 4.7 patch header files for wchar.h, stdio.h, os_defines.h
and re downloading the latest codeblocks version.
but nothing makes it seem to work.
what settings do I need to change to make this work.
#include <iostream>
#include <string>
using namespace std;
int main()
{
string s=to_string(10);
cout <<s<<endl;
return 0;
}
update: I have re-installed codeblocks,version 13.12 tmd gcc, with gcc version 4.81(tmd-2), I have both -std=c++0 and -std=c++11 flags on in compiler settings, in Toolchain executables i have c++ compiiler:mingw32-gcc-4.8.1.exe, and it Still does not recognize to_string();
I suppose you are using the Code::Blocks MinGW distribution which includes GCC 4.7.1. The MinGW implementation of libstdc++ does not support to_string, this is a known bug which cannot be fixed by simply patching the header files. Upgrade to the latest Code::Blocks version with GCC 4.8.1.