Reputation: 931
I had this problem before but found a work around, only this time a work around is not an option.
I'm trying to use the 'stof' function but I'm getting errors saying: 'stof' is not a member of 'std' Function 'stof' could not be resolved
I'm using it in the exact way if shows on this page:http://www.cplusplus.com/reference/string/stof/
And here are my includes:
#include <iostream>
#include <sstream>
#include <string>
using namespace std;
So, what am I doing wrong? And if a solution cannot be found could someone point me to another way to convert string to float and have it throw an exception if the string was not compatible?
EDIT: Updating with sample program and errors.
#include <iostream>
#include <sstream>
#include <string>
using namespace std;
int main()
{
string testString = "";
float testFloat = 0.0;
testFloat = std::stof(testString);
return 0;
}
The errors I get are:
Description Resource Path Location Type 'stof' is not a member of 'std' main.cpp /Assignment/src line 33 C/C++ Problem
Description Resource Path Location Type Function 'stof' could not be resolved main.cpp /Assignment/src line 33 Semantic Error
Upvotes: 7
Views: 23870
Reputation: 35208
You're using stof
correctly. This is a known bug in the MinGW build of gcc 4.7.2. It should be fixed for gcc 4.8. There are numerous examples on SO for converting strings to numbers. Here's one. You'll have to roll your own to get the exception behavior you want, but the examples should get you started.
Upvotes: 8
Reputation: 15693
stof is a C++11 function. Make sure your compiler supports it (no compiler has full support for C++11 yet, though most modern compilers out there right now support a fairly large subset).
On g++ for instance you have to enable it with the -std=c++11
option (std=c++0x
pre g++-4.7).
If you're using g++, please check which version you're using with g++ -v
- if it's an old version (like 4.2 for instance) c++11 functionality won't be available.
Upvotes: 12