std::stod is not a member of std [duplicate] - c++

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>

Related

C++ Complex library compile error

Here is my code:
#include <iostream>
#include <iomanip>
#include <complex>
#include <cmath>
int main()
{
using namespace std::complex_literals;
std::cout<< std::fixed<<std::setprecision(1);
std::complex<double> z1= 1i *1i; //imaginary unit squared
std::cout<<"i * i= " <<z1 <<'\n';
std::complex<double> z2=std::pow(1i, 2); //imaginary unit squared
std::cout <<"pow(i,2) =" <<z2 <<'\n';
}
I compile with gcc hello.cpp -lstdc++ -o hello.o
Basically it won't let me double the powers of a complex number when i do std::complex z2=std::pow(1i, 2);..
I get the following error
error: no matching function for call to 'pow(complex int, int)'
std::complex z2=std::pow(1i, 2);
However, if i remove the complex number and do std::complex z2=std::pow(2, 2);
it returns 4, the correct answer..
There are many more lines of compile errors, but i made it brief
This answer follows up the comments to the original question:
you have to force the compiler to use the c++14 standard with the -std=c++14 option because the literal operator""i is part of the C++14 spec.
GCC uses c++14 by default since version 6.1. Check your compiler version with gcc -v and refer to this link for GCC standard support.
EDIT:
I was able to reproduce the compiling issue with GCC 6.3 through the link provided by Mr Richard Critten in the comments to the original question, who was the first to point to the correct answer. My apology because I totally overlooked the reference to the C++14 standard.
Anyway, for the sake of clarity, I'm editing this answer, because I've found something that may be interesting to share.
The reason why compiling with GCC 6.3 fails is the fact that the reference standard has been changed in December 2016 from C++14 to GNU++14, see here.
GNU++14 is an extension to the C++ standard, that, among other things, provides additional functions overload for standard APIs.
I've found that with GNU++14 SFINAE fails in finding a proper overload for the std::pow() functions unless the type is explicitly set in the template call like in the snipped below:
std::complex<double> z2=std::pow<double>(1i, 2);
The GNU++14 includes changes to the cmath and complex header files, that I believe are the cause of the issue.
Turning on the C++14 flag, that is not the default anymore, fixes the problem.
I don't know why it is not compiling on your system, it runs fine on mine.
I think something is wrong with your compiler. And, not the version. yet you could try std::cout << __cplusplus. See what this prints.
C++11 doesn't recognize std::complex_literals, so doubt that is the case, but yet the compiler still couldn't find the function.
I honestly don't understand why it is searching for (complex int, int). The compiler is either corrupt or an old beta version.
You should either download a fresh version of the compiler or run it online somewhere. Try adding -std=c++14, but I doubt that would help.
Try your code here(it works):
https://www.jdoodle.com/online-compiler-c++14
So, I installed Microsoft Visual Studio.. I am using the clang compiler and my program ran smoothly with no errors!
Thanks for all the help guys, but i think clang is a better c++ compiler

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.

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

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.

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.

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.