Stoi and stoll in C ++

I have #include (string) in my declarations at the top of the program, but when I try to run stoi (string) or stoll (string), I get the following error. I am running Cygwin g ++ v4.5.3.

Z: \ G \ CSCE 437> g ++ convert.cpp -o conv convert.cpp: In function void transfer(std::string*)': convert.cpp:103:36: error:

stoll 'was not declared in this scope convert.cpp: 116: 35: error: `stoi' was not declared in this scope

    fileTime[numRec] = stoll(result[0]);    //converts string to Long Long
    if(numRec = 0){
       beginningTime = fileTime[0];
    }
    fileTime[numRec] = timeDiff;
    hostName[numRec] = result[1];
    diskNum[numRec] = stoi(result[2]);
    type[numRec] = result[3];
    offset[numRec] = stoi(result[4]);
    fileSize[numRec] = stoi(result[5]);
    responseTime[numRec] = stoi(result[6]);`

      

Where the result is an array of strings.

+3


source to share


1 answer


These features are new in C ++ 11, and GCC only makes it available if you specify that version of the language using the command line option -std=c++11

(or -std=c++0x

in some older versions, I think this will be needed for version 4.5).

If you cannot use C ++ 11 for some reason, you can convert using string streams:



#include <sstream>

template <typename T> from_string(std::string const & s) {
    std::stringstream ss(s);
    T result;
    ss >> result;    // TODO handle errors
    return result;
}

      

or, if you feel masochistic C functions such as those strtoll

declared in <cstring>

.

+10


source







All Articles