No Member named stoi in namespace std
Solution 1
First of all, you need a compiler that supports C++11 and you need to compile in "C++11 mode" (in some cases).
Secondly, if this is in fact an intellisense issue (and it looks like it may be), then it could simply be that your IDE doesn't support C++11 yet.
Solution 2
std::stoi
is available only since C++11. In case you don't have C++11 support, here's the C++03 solution based on std::istringstream
:
std::string test = "45";
std::istringstream is(test);
int myInt;
if (is >> myInt)
std::cout << myint << std::endl;
you just need to #include <sstream>
Solution 3
If you can use stdlib.h
, then other way to make it work is to use atoi(const char *)
int myint = atoi(test.c_str());
Related videos on Youtube
user2211678
Updated on September 15, 2022Comments
-
user2211678 about 1 year
I am testing out
std::stoi
function found in the link below: http://en.cppreference.com/w/cpp/string/basic_string/stol
but I got the error:No Member named stoi in namespace std.
What should I do? Please advise thanks.
P.S: I am using Xcode Ide to do my c++.
#include <iostream> #include <string> int main() { std::string test = "45"; int myint = std::stoi(test); std::cout << myint << '\n'; }
Image
-
David G about 10 yearsDid you compile with
-std=c++11
?
-
-
Admin about 10 yearsAlso,
std::strtol()
. -
LihO about 10 years@H2CO3: Yeah, but that's taking C-style
char*
and it's in<cstdlib>
. But good point :) -
Pablo Ariel about 5 yearsI have C++14 support and I have the same issue.
-
Pablo Ariel about 5 yearsI have C++14 support and I have the same issue. It's clearly related to CLang or its components. Nothing to do with the language version setting though.
-
Lightness Races in Orbit about 5 years@PabloAriel You probably missed something. While there are gaps in support for C++17 in Xcode (e.g. no
std::optional
) even in C++17 mode, C++14 is old enough now and I am not aware that support for something as core asstd::stoi
is missing. -
Pablo Ariel about 5 yearsThe problem comes because of this:
#if ((__cplusplus >= 201103L) && defined(_GLIBCXX_USE_C99) && !defined(_GLIBCXX_HAVE_BROKEN_VSWPRINTF))
However, nobody mentioned a solution other than defining _GLIBCXX_USE_C99 and nobody provided an explanation on why should I add such definition manually (looks like a bug to me though). -
Lightness Races in Orbit about 5 years@PabloAriel It's not really clear what "this" you are talking about, or what you are trying to do, or what platform/toolchain/configuration you are using. I suggest collecting the necessary information then asking a fresh question. It's unlikely to be a bug in anything other than your own code/configuration. coliru.stacked-crooked.com/a/ccda550cce24a6c9
-
Pablo Ariel about 5 yearsWhat is unlikely is my code having more bugs than the MLoCs of codebase making visual studio combined with the android ndk and clang. Also I make heavy use of C++14 and C++17 features so it makes no sense it fails only with stoi. You can find the "#if" I've quoted in <basic_string.h> line 2847 or so. Also it seems a lot of people had the same problem: google I better use a custom implementation written by myself. Thanks anyway.
-
Lightness Races in Orbit about 5 years@PabloAriel Your code having more bugs than Visual Studio combined with the Android NDK and Clang is entirely likely.
-
Lightness Races in Orbit about 5 years@PabloAriel You know best then. Good luck.
-
Lightness Races in Orbit about 5 years@PabloAriel: Great!