Using stoi with c++11

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?
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.
1
2
3
 getline(file,Size_);
 int Setsize = stoi (Size_);
 Map.resize(Setsize);
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
thanks, why do you think I am getting this error?
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
I tried that and it didnt work. Would there be another fix?
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.
> Would there be another fix?

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