Using stoi with c++11

Dec 5, 2015 at 6:25pm
I am trying to convert a string to int using stoi. My IDE is codeblocks and I made sure I am using the c++11 compiler but I am still getting this error:
error: 'stoi' was not declared in this scope
Does anyone know of a fix for this?
Dec 5, 2015 at 6:26pm
Would be helpful if you showed us your code, we can't read your mind in a public place like this, not without our masks.
Dec 5, 2015 at 6:35pm
1
2
3
 getline(file,Size_);
 int Setsize = stoi (Size_);
 Map.resize(Setsize);
Dec 5, 2015 at 6:42pm
Work-around, assuming that std::strtol() is available in the library:
http://en.cppreference.com/w/cpp/string/byte/strtol

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#include <cstdlib>
#include <cerrno>
#include <string>
#include <limits>
#include <stdexcept>

namespace temp_fix
{
    int stoi( const std::string& str, std::size_t* pos = 0, int base = 10 )
    {
        const char* begin = str.c_str() ;
        char* end = nullptr ;
        long value = std::strtol( begin, &end, base ) ;

        if( errno == ERANGE || value > std::numeric_limits<int>::max() || value < std::numeric_limits<int>::min() )
            throw std::out_of_range( "stoi: out ofrange" ) ;

        if( end == str.c_str() )
            throw std::invalid_argument( "stoi: invalid argument" ) ;

        if(pos) *pos = end - begin ;

        return value ;
    }
}

http://coliru.stacked-crooked.com/a/e2470f31612956fc
Dec 5, 2015 at 6:45pm
thanks, why do you think I am getting this error?
Dec 5, 2015 at 7:06pm
The compiler/library that ships with CodeBlocks is a bit dated.

You can switch to a more current implementation; one that would have std::stoi()
http://www.cplusplus.com/forum/beginner/149018/#msg780212
Dec 5, 2015 at 7:40pm
I tried that and it didnt work. Would there be another fix?
Dec 5, 2015 at 7:49pm
Did you set the option for C++11 support? It should be available in Project->Build Options and then check "Have g++ follow the C++11 ISO C++ language standard" (or go to Settings->Compiler to set it as default for all projects). If you did, could you post the build log of your program? That way we can see all options passed to the compiler and the exact error it gives. It's easier to diagnose it that way.
Dec 5, 2015 at 8:15pm
> Would there be another fix?

Try this: http://www.cplusplus.com/forum/general/152105/#msg791139
Dec 5, 2015 at 8:43pm
closed account (48T7M4Gy)
http://stackoverflow.com/questions/8542221/stdstoi-doesnt-exist-in-g-4-6-1-on-mingw
Topic archived. No new replies allowed.