stoi and c++ 11

Dec 29, 2013 at 6:37pm
Hi.
I would like to use the function stoi, so I could convert a string to an int more easily. The problem is that everytime I try to use it, I get an error message saying: "'stoi' is not a member of 'std'".
I use Codeblocks and GNU gcc Compiler. I've always programmed in c++ instead of c++11 until now. I saw on the internet that to switch for c++11 in codeblocks, one just has to go to Project->Build Options->Compiler Flags and then enable:
-Have g++ follow the coming c++Ox ISO C++ language standard[-std=c++Ox];
-Have g++ follw the c++11 ISO C++ language standard[-std=c++11]
Have I done something wrong?

I really need help.
Thanks in advance

Also, one example of the codes that I write and then get a mistake is the following:
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 <iostream>

using namespace std;
#include <string>
#include<sstream>


int main()
{
    std::string str1 = "45";
    std::string str2 = "3.14159";
    std::string str3 = "31337 with words";


    int myint1 = std::stoi(str1);
    int myint2 = std::stoi(str2);
    int myint3 = std::stoi(str3);


    std::cout << "std::stoi(\"" << str1 << "\") is " << myint1 << '\n';
    std::cout << "std::stoi(\"" << str2 << "\") is " << myint2 << '\n';
    std::cout << "std::stoi(\"" << str3 << "\") is " << myint3 << '\n';

}
  
Dec 29, 2013 at 7:05pm
What operating system are you using? And what is the exact version of gcc?

Dec 29, 2013 at 7:10pm
Enable 'full compiler command line logging' code blocks and see what exactly is passed to g++.
Dec 29, 2013 at 7:11pm
The C++11 string conversion functions (to_string, stoi, stol, etc.) don't work in MinGW (which is what comes with Code::Blocks by default).

You'll have to use a different method to convert strings to integers.
Take a look here:
http://www.cplusplus.com/articles/D9j2Nwbp/
Dec 29, 2013 at 7:33pm
I use Code::Blocks and it works for me. You need MinGW 4.8.1.
Dec 29, 2013 at 7:36pm
I think it works on 64-bit MinGW, but not the 32-bit version.

I have MinGW 4.8.1 (32-bit) and it doesn't work for me.
Dec 29, 2013 at 7:41pm
@ long double main
I am on 32. Did you setup Code::Blocks to use it?
Dec 29, 2013 at 7:58pm
I use Code::Blocks and it works for me. You need MinGW 4.8.1.

Can I simply uninstall the MinGW version that I have and install MinGW 4.8.1?
(My operating system is windows vista 32-bit)
Last edited on Dec 29, 2013 at 8:01pm
Dec 29, 2013 at 8:13pm
You only need to install 4.8.1 but then you need to tell Code::Blocks where the files are.In settings/compiler/Toolchain executables.
Dec 29, 2013 at 11:01pm
I am on 32. Did you setup Code::Blocks to use it?

That shouldn't matter, should it?

I just tried it out with TDM-GCC 4.8.1 (the 32-bit version), and I get the same error about those functions not being in namespace std.

Now, it works for me if I use TDM-GCC 4.8.1 (the 64-bit version) and have it compile in 32-bit mode (since my computer is 32-bit).

(I tested using TDM-GCC since that's what Code::Blocks comes with; my actual MinGW installation is the one from mingw.org.)
Dec 30, 2013 at 2:15am
You only need to install 4.8.1 but then you need to tell Code::Blocks where the files are.In settings/compiler/Toolchain executables.

If I change only Compiler's instalation directory CodeBlocks tells me the following:

"The compiler's setup (GNU GCC Compiler) is invalid, so Code::Blocks cannot find/run the compiler.
Probably the toolchain path within the compiler options is not setup correctly?!
Goto "Settings->Compiler and debugger...->Global compiler settings->GNU GCC Compiler->Toolchain executables" and fix the compiler's setup.
Skipping..."

and I don't even get error messages(nor manage to run the program, obviously)

What also do I have to change in Toolchain executables?
Thanks in advance
Dec 30, 2013 at 3:01am
Are you using version 4.8.1?

I think this is a known bug for the 32 bit version. At least that's what I've found on google.

Dec 30, 2013 at 3:08am
closed account (j3Rz8vqX)
If you're still stuck, you might as well use one of the available binaries.

codeblocks binaries download page:
http://www.codeblocks.org/downloads/binaries

(To anyone who may not know how to install codeblocks for windows)
Remember, you'll want the mingw+setup executable.


NOTE: The codeblocks-13.12mingw-setup.exe file includes the GCC compiler and GDB debugger from TDM-GCC (version 4.7.1, 32 bit). The codeblocks-13.12mingw-setup-TDM-GCC-481.exe file includes the TDM-GCC compiler, version 4.8.1, 32 bit. While v4.7.1 is rock-solid (we use it to compile C::B), v4.8.1 is provided for convenience, there are some known bugs with this version related to the compilation of Code::Blocks itself.


GCC 4.7.1+ should support most c++11 functions

New GCC features compatibility list:
http://gcc.gnu.org/projects/cxx0x.html

To cut it short, 4.8.1 additionally supports:

Rvalue references for *this //4.8.1

//4.8.0
Alignment support
Inheriting constructors
Bidirectional Fences
Abandoning a process and at_quick_exit
Thread-local storage


Dec 30, 2013 at 5:40am
@DPut
I don't think the "pure" 32-bit binaries of MinGW work. I tried it with TDM-GCC 32 bit (4.8.1) and it doesn't work, but if you use MinGW it works (I used i686-w64-mingw-32).

Slightly offtopic, why not use int myint1 = lexical_cast<int>(str1);?
Dec 30, 2013 at 7:32am
NT3 wrote:
Slightly offtopic, why not use int myint1 = lexical_cast<int>(str1);?
Because that is from boost only and some people don't like adding large dependencies to their projects just to do simple things?
Dec 30, 2013 at 7:50am
Is it? I don't even have boost on my system and it works for me...
Dec 30, 2013 at 7:56am
I tried stoi with MinGW 4.8.1 and it doesn't work for me either. Why not get a different compiler that supports C++ 11? I used Visual C++ 2013 and tried stoi on there and it works. This is the code I tried:
1
2
3
4
5
6
7
8
9
10
#include <iostream>
#include <string>

int main()
{
    std::string str = "12345";
    int s_str = std::stoi(str);
    std::cout << "The string is now an int: " << s_str << std::endl;
    return 0;
}
Last edited on Dec 30, 2013 at 7:56am
Dec 30, 2013 at 8:02am
@Stormboy
What build of MinGW?
Dec 30, 2013 at 8:09am
@ NT3: Its the one I downloaded from http://www.mingw.org/. The version is: 4.8.1-4.

@ franciscoat: Why not make your own conversion function? The one below works fine.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream>
#include <string>
#include <cstdlib>

int toint(std::string s) //The conversion function
{
    return atoi(s.c_str());
}

int main()
{
    std::string str = "12345";
    int intx = toint(str); //Convert str to an integer
    std::cout << "The string is now an int: " << intx << std::endl; //output it to the screen
    return 0;
}
Last edited on Dec 30, 2013 at 8:21am
Dec 30, 2013 at 8:23am
> I would like to use the function stoi, so I could convert a string to an int more easily.
> The problem is that everytime I try to use it, I get an error message saying: "'stoi' is not a member of 'std'".

Assuming that std::strtol() is available in the library, we can write a work-around:

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
#include <iostream>
#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() )
            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 ;
    }
}

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

    for( const std::string& str : test )
    {
        try
        {
            std::size_t pos = 0 ;
            std::cout << "str: " << str << " => " ;
            int value = temp_fix::stoi( str, &pos, 0 ) ;
            std::cout << "  value: " << value << "  pos: " << pos << '\n' ;
        }
        catch( const std::exception& e )
        {
            std::cerr << "*** error: " << e.what() << '\n' ;
        }
    }
}

http://coliru.stacked-crooked.com/a/e5124a6d7f5d47b8
Topic archived. No new replies allowed.