Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

cygwin g++ std::stoi "error: ‘stoi’ is not a member of ‘std

Tags:

c++11

g++

cygwin

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?

like image 941
user3021700 Avatar asked Nov 22 '13 12:11

user3021700


People also ask

What library is stoi?

std::stoi is a standard library function, not a keyword. A keyword is something like for or new .

What does stoi return if fails?

If no conversion could be performed, zero is returned. The previous reference said that no conversion would be performed, so it must return 0.

What is the difference between Atoi and stoi?

First, atoi() converts C strings (null-terminated character arrays) to an integer, while stoi() converts the C++ string to an integer. Second, the atoi() function will silently fail if the string is not convertible to an int , while the stoi() function will simply throw an exception.

What does stoi return?

Since stoi returns the integer value if parsed you can't directly use the return value to check for correctness. You could catch std::invalid_argument exception but it could be too much.


3 Answers

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

like image 103
user3125280 Avatar answered Oct 23 '22 23:10

user3125280


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.

like image 35
ShuaiYu8 Avatar answered Oct 24 '22 00:10

ShuaiYu8


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.

like image 24
Josh Avatar answered Oct 23 '22 23:10

Josh