6

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

no member named stoi in namespace 'std'

error

LihO
  • 41,190
  • 11
  • 99
  • 167
user2211678
  • 669
  • 8
  • 13
  • 24

3 Answers3

10

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>

LihO
  • 41,190
  • 11
  • 99
  • 167
7

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.

Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055
  • I 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. – Pablo Ariel Aug 22 '18 at 18:11
  • 1
    @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 as `std::stoi` is missing. – Lightness Races in Orbit Aug 22 '18 at 18:15
  • The 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). – Pablo Ariel Aug 22 '18 at 18:34
  • @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. http://coliru.stacked-crooked.com/a/ccda550cce24a6c9 – Lightness Races in Orbit Aug 23 '18 at 11:41
  • What 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 line 2847 or so. Also it seems a lot of people had the same problem: [google](https://www.google.com.ar/search?q=_GLIBCXX_USE_C99) I better use a custom implementation written by myself. Thanks anyway. – Pablo Ariel Aug 24 '18 at 21:40
  • @PabloAriel Your code having more bugs than Visual Studio combined with the Android NDK and Clang is _entirely_ likely. – Lightness Races in Orbit Aug 28 '18 at 10:41
  • Weird... when I report bugs to MS they seem to be happy and derive them for fixing instead of telling me my code is wrong. Maybe you should tell them they're wrong. Besides, it's fairly visible how mismanaged are those projects and if you know anything about statistical analysis you could easily note how the bigger the code the more the bugs. Also probably any project that is more than 1mloc is heavily mismanaged, unless it's a program that literally does everything you can imagine. – Pablo Ariel Aug 28 '18 at 18:36
  • 1
    @PabloAriel You know best then. Good luck. – Lightness Races in Orbit Aug 28 '18 at 18:46
  • No need for luck... I'd rather bet on knowledge and experience, but thank you anyway. – Pablo Ariel Aug 28 '18 at 19:40
  • 1
1

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());
Makesh
  • 1,236
  • 1
  • 11
  • 25