stoi() in Code::Blocks 13.12 MinGW/GCC (4.8.1)

Hy guys,

Here is my code.

#include <iostream>
#include <string>

int main() {
string testnumber = "10";
int myInt = stoi(testnumber);
cout << myInt;

return 0;
}

Here is my error message: error: 'stoi' was not declared in this scope

I have to work with Code::Blocks 13.12 MinGW/GCC (4.8.1).
I am not allowed to change the program and the compiler either.
Has anyone a solution for this, please ?

Thank you

Last edited on
Do you have C++11 switched on? (with -std=c++11)
Yes, I switched in the command flags but still not work
Last edited on
got the same error
As a temporary fix, use conversion via string streams.

For instance, assuming a decimal base (does not have the full functionality of std::stoi):

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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#include <string>
#include <sstream>
#include <iostream>
#include <limits>
#include <stdexcept>

namespace my
{
    int stoi( std::string str )
    {
        std::istringstream stm(str) ;
        long long v = 0 ;

        if( stm >> v )
        {
            if( v < std::numeric_limits<int>::min() || v > std::numeric_limits<int>::max() )
                throw std::out_of_range( "my::stoi: out of range" ) ;
            return v ;
        }

        else throw std::invalid_argument( "my::stoi: invalid argument" ) ;
    }

    template < typename T > std::string to_string( const T& v )
    {
        std::ostringstream stm ;
        stm << v ;
        return stm.str() ;
    }
}

int main()
{
    const std::string test[] = { "1234", "-1234", "12345a6", "a1234", "122345678912" } ;

    for( const std::string& str : test )
    {
        try
        {
            std::cout << "str: " << str ;
            int v = my::stoi(str)  ;
            std::cout << " =>  " << v << " => " << my::to_string(v) << '\n' ;
        }
        catch( const std::exception& e )
        {
            std::cerr << "*** error: " << e.what() << '\n' ;
        }
    }
}

http://coliru.stacked-crooked.com/a/5b8a5563b07dd3fa
Try atoi()
tks so much for share
Odd report... anyways...

complimentary post about my favorite library for conversions, cppformat: https://github.com/cppformat/cppformat

If you just want it to work: http://ideone.com/m0fPzz

If that exact verbatim example doesn't work (along with the appropriate "-std=c++11" flag), then perhaps MinGW (or rather, that version of MinGW) doesn't handle stoi. Try using a more up to date environment and compiler.

Did some research, that version does support stoi. You're doing something wrong.
Last edited on
Topic archived. No new replies allowed.