Hi
When I tried to compile like g++ -std=c++11 -W -Wall
it gives this error
error: ‘stoi’ is not a member of ‘std’

#include <string>
#include <iostream>
#include <fstream>

class Grade {
    public:

        // Default Constructor  
        Grade() {};

        // Functors
        // If the given course and the score matches the student grade, return true
        bool operator()(std::string course, std::string score) { 
            return course == course_ && std::stoi(score) == score_; 
            //return false;
        }
        t

this is not complete code.any suggestions

    Any suggestions?

the function stoi does not exist. The function you probably meant to use is atoi. And you need to include the <cstdlib> for that one.

What version of gcc are you using? stoi exists in C++11, but that doesn't mean your compiler is sure to support it.

Also stoi() throws an exception on error and your program should process it in a try/catch block.

Edited 3 Years Ago by Ancient Dragon

$ g++ -std=c++11 -W -Wall source.cpp
In file included from Student.h:6:0,
                 from source.cpp:5:
Grade.h: In member function ‘bool Grade::operator()(std::string, std::string)’:
Grade.h:15:42: error: ‘stoi’ was not declared in this scope
    return course == course_ && stoi(score) == score_;
                                          ^
Grade.h: In member function ‘bool Grade::operator()(std::string, std::string, st                                                                                                                d::string)’:
Grade.h:20:43: error: ‘stoi’ was not declared in this scope

this is the error message on 4.8.1 gcc

Sorry that I initially didn't know that stoi existed (it's new).

The stoi function exists at least since GCC 4.6.0, that's how far back I can check, it might have existed even before that.

So, there must be something else going on here. I don't know what.

Try adding -std=c++0x as sometimes, this can cause problems depending on the compiler. What is the operating system you're currently working on?

Hi, so i tried a small test on my machine, i compiled the following code

#include <string>
#include <iostream>

int main( void )
{
    std::string s("56");
    std::cout << "As string: " << s << " As int " << std::stoi(s) << std::endl;

    return 0;
}

Im using Ubuntu 12.04lts so i only have an older version of g++

gcc version 4.6.3 (Ubuntu/Linaro 4.6.3-1ubuntu5)

So i could not use -std=c++11 instead i compiled it as

g++ -std=c++0x test.cpp

And this worked just fine, i dug a bit further and found within <string> there is an include for <bits/basic_string.h> within this file i found the control #defines for the code block with stoi

#if (defined(__GXX_EXPERIMENTAL_CXX0X__) && defined(_GLIBCXX_USE_C99) \
     && !defined(_GLIBCXX_HAVE_BROKEN_VSWPRINTF))

#include <ext/string_conversions.h>

namespace std _GLIBCXX_VISIBILITY(default)
{
_GLIBCXX_BEGIN_NAMESPACE_VERSION

  // 21.4 Numeric Conversions [string.conversions].
  inline int
  stoi(const string& __str, size_t* __idx = 0, int __base = 10)
  { return __gnu_cxx::__stoa<long, int>(&std::strtol, "stoi", __str.c_str(),
                    __idx, __base); }

Within eclipse i can see what #defines are set, the only thing that was not set by default was GXX_EXPERIMENTAL_CXX0X (so the -std=c++0x enabled it) .If you have eclipse, checking whats set or not in the file or seeing if you can find in your compilers headers the function prototype for stoi, hopefully you can figure out why its not being found.

Im afraid that it may be a slow and painful process but its the only thing i can think of to suggest to you.

This article has been dead for over six months. Start a new discussion instead.