trappings of stof()

Please tell me how to keep stof. Ive read it to be problematic. I am getting an error
 
\src\main.cpp:18:9: error: 'stof' is not a member of 'std'

I've made sure that my compiler flags correctly:
-std=c++11

source code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream>
#include <string>
#include <sstream>
#include <string>
#include <cstdlib>

using namespace std;

float get_float(string message)
{
	return std::stof(message);
}
int main()
{
    std::string str_value;
    std::cout << "Enter a numeric string:";
    std::cin >> str_value;

    float f1Value = 0.0f;
    f1Value = get_float(str_value);
    std::cout<<"literal converted value (float):"<<f1Value;

}


If you can tell me how to keep using it I would be very appreciative!
What compiler are you using? And don't forget to tell us the version of that compiler as well.


From the use of backslashes in your path I am guessing you're on Windows, and from the fact it does not work, I am guessing you're using a very old distribution of mingw. Try a modern one from https://nuwen.net/mingw.html or http://mingw-w64.org or just use Visual Studio (it's also free, and is native to your platform)
Last edited on
If you use the Code::Blocks IDE, you have to overwrite your three header files before using these functions. Try this http://tehsausage.com/mingw-to-string
Last edited on
I am using Eclipse IDE, yes windows. Surely there is some solution that doesn't require such a seismic shift for such a small problem. : )

Oh, how do I find out my minGW version?
Surely there is some solution that doesn't require such a seismic shift for such a small problem. : )

If your compiler doesn't support stoX() then there is always the always available stringstream approach to convert to strings.

I believe the stoX() functions require a 5.0 or higher version of the g++ compiler and if your compiler doesn't support this function then you will need to either upgrade the compiler or find a compiler that does support this feature.
One way to do it:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <sstream>
#include <stdexcept>

float get_float(std::string& message)
{
  std::stringstream ss(message);
  float value;

  ss >> value;
  if (!ss)
    throw std::invalid_argument(message);

  //TODO check for ot of range error

  return value;
}


Alternativ you can use the good old atof, though it returns a double
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream>
#include <string>
#include <cstdlib>

double get_double(std::string message)           // decide whether to qualify string with std:: or not
{
// return std::stod(message);                    // update your compiler
   return std::strtod( message.c_str(), 0 );     // sledgehammer job for older compilers
}


int main()
{
    std::string str_value;
    std::cout << "Enter a numeric string: ";
    std::cin >> str_value;

    double f1Value = get_double(str_value);
    std::cout<<"literal converted value (double): " << f1Value;
}
Last edited on
> how do I find out my minGW version?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream>

int main()
{
    #ifdef __MINGW32__
        std::cout << "mingw32  : " << __MINGW32_MAJOR_VERSION << '.' << __MINGW32_MINOR_VERSION << '\n' ;
    #endif // __MINGW32__

    #ifdef __MINGW64__
        std::cout << "mingw64  : " << __MINGW64_VERSION_MAJOR << '.' << __MINGW64_VERSION_MINOR << '\n' ;
    #endif // __MINGW64__

    #ifdef __GNUC__
        std::cout << "g++      : " << __GNUC__ << '.' << __GNUC_MINOR__ << '.' << __GNUC_PATCHLEVEL__ << '\n'
                  << "libstdc++: " << _GLIBCXX_RELEASE << " (release date " << __GLIBCXX__ << ")\n" ;
    #endif // __GNUC__
}
> how do I find out my minGW version?

getting an error with the version type algorithm above.



..\src\main.cpp:21:39: error: '_GLIBCXX_RELEASE' was not declared in this scope
                   << "libstdc++: " << _GLIBCXX_RELEASE << " (release date " << __GLIBCXX__ << ")\n" ;



I'll continue to work on that and with the other comments.

my includes
1
2
3
4
5

#include <iostream>
#include <string>
#include <sstream>
#include <cstdlib> 


Lets see whether a compiler change is necessary.

For everyone suggesting a workaround, here is a sample code that needs a change:

1
2
\src\main.cpp:573:82: error: 'stof' was not declared in this scope
                vertex.y = stof(dataLine.substr(foundStart, foundEnd - foundStart));

I'm not familiar with atof, but tried it anyway:
1
2
.\src\main.cpp:569:87: error: cannot convert 'std::__cxx11::basic_string<char>' to 'const char*' for argument '1' to 'double atof(const char*)'
                vertex.x = std::atof(dataLine.substr(foundStart, foundEnd - foundStart));
Last edited on
mingw32 : 3.22
g++ : 5.3.0
libstdc++:

I was only able to get this by commenting out (see below) -- for part of the answer.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream>
#include <string>
#include <sstream>
#include <cstdlib>

int main()
{
    #ifdef __MINGW32__
        std::cout << "mingw32  : " << __MINGW32_MAJOR_VERSION << '.' << __MINGW32_MINOR_VERSION << '\n' ;
    #endif // __MINGW32__

    #ifdef __MINGW64__
        std::cout << "mingw64  : " << __MINGW64_VERSION_MAJOR << '.' << __MINGW64_VERSION_MINOR << '\n' ;
    #endif // __MINGW64__

    #ifdef __GNUC__
        std::cout << "g++      : " << __GNUC__ << '.' << __GNUC_MINOR__ << '.' << __GNUC_PATCHLEVEL__ << '\n'
                  << "libstdc++: "; //<< _GLIBCXX_RELEASE << " (release date " << __GLIBCXX__ << ")\n" ;
    #endif // __GNUC__
}
1
2
// vertex.x = std::atof(dataLine.substr(foundStart, foundEnd - foundStart));
vertex.x = std::atof( dataLine.substr(foundStart, foundEnd - foundStart).c_str() );

Ideally switch to a current 64-bit version of MinGW (See Cubbi's post for links).
Will it be backwards compatible?

What will it do to my 32bit installation, and how does my Eclipse IDE find it?

I guess my questions orbit around upgrading my compiler.

Better worded: how do I tell my Eclipse IDE to stop paying attention to the current 32 bit installation and to use the new 64bit?
Last edited on
> What will it do to my 32bit installation

To keep the existing 32bit installation intact, install the new version of MinGW in to a different directory.


> how do I tell my Eclipse IDE to stop paying attention to the current 32 bit installation and to use the new 64bit?

Consider posting this question on an Eclipse/CDT form.
Right, thank you.
Well, I'm back! I've installed MingW64 as per above and have been compiling 64bit programs for a while now. Very cool. The successful compiler change didn't fix the pesky atof error though:

error

1
2
3
..\src\main.cpp: In function 'void readObj(std::__cxx11::string)':
..\src\main.cpp:569:87: error: cannot convert 'std::__cxx11::basic_string<char>' to 'const char*' for argument '1' to 'double atof(const char*)'
           vertex.x = std::atof(dataLine.substr(foundStart, foundEnd - foundStart));

Any thoughts?
You're trying to feed a C++ string to the C function atof. It's either std::stof(dataLine.substr(foundStart, foundEnd - foundStart)); or std::atof(dataLine.substr(foundStart, foundEnd - foundStart).c_str());
Last edited on
Yeah, I'm weak on c strings/functions.
That worked, thank you.
Topic archived. No new replies allowed.