18

I have:

-cygwin 1.7.25 on windows 7/32bit

-g++ --version --> g++ (GCC) 4.8.2

-libstdc++.a --> gcc-g++-4.8.2-1

Tried to make a c++ Hello World:

#include <string>

int main() 
{
   std::string s = "123";
   int i = std::stoi(s);
}

compiling gives:

$ g++ -std=c++11 main.cpp
main.cpp: In function ‘int main()’:
main.cpp:6:10: error: ‘stoi’ is not a member of ‘std’
  int i = std::stoi(s);

I searched for hours but I still could not find a solution. What's the issue here?

stefan
  • 10,215
  • 4
  • 49
  • 90
user3021700
  • 181
  • 1
  • 1
  • 3

5 Answers5

12

That's a bug, possibly an incomplete port of some library code to cygwin (it's a cplusplus11 feature) - some stuff has to be changed after all. Make sure to report it.

The solution is easy of course: #include <cstdlib> strtol(s.c_str(),0,10);

www.cplusplus.com/.../strtol

A similar mingw bug is mentioned also here

std::stoi doesn't exist in g++ 4.6.1 on MinGW

Community
  • 1
  • 1
user3125280
  • 2,779
  • 1
  • 14
  • 23
3

I have the same problem yesterday. "error: 'stoi' is not a member of 'std'."

First, I made sure c++11 was enabled. Then, I updated the g++ compiler to the newest version. After that, this error disappeared.

ShuaiYu8
  • 199
  • 2
  • 6
1

The compiler is not being taken seriously. On windows your best bet is to probably use visual studio, as it is always kept up to date . The bug here is that the macro defs are wrong to begin with. The problem starts from iomanip.h and iosbase . So they would have to changed all of there code. There are user made patches for this but I would not trust them at all, as they may contain even more bugs then the original copies. But it's up to you , I just stick with visual studio express edition.

Josh
  • 19
  • 1
0

stoi works correct only on mingw64 for me. If you use Codeblocks, don't forget to check if your projects compiler is set to mingw64.

srrr21
  • 1
0

Well, I am working with -std=c++98, not -std=c++11 but I solved it with the following:

int i = std::atoi(input.c_str());

atoi() is waiting for c type null-terminated string, c_str() makes it null-terminated char*. To use atoi I also() added the following library:

#include <cstdlib>

my system is:

Ubuntu 22.04.1 LTS
MM Temel
  • 38
  • 4