Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Function 'stof' could not be resolved

Tags:

c++

c++11

mingw

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

like image 264
Joseph Little Avatar asked Feb 15 '13 20:02

Joseph Little


1 Answers

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.

like image 162
Cubic Avatar answered Oct 14 '22 15:10

Cubic