質問

All indications tell me this is a ridiculously easy problem to solve, but I can't figure out error telling me the atoi function doesn't exist.

C++

#include <iostream>
#include <stdlib.h>

using namespace std;

string line;
int i;

int main() {

    line = "Hello";
    i = atoi(line);
    cout << i;

    return 0;
}


Error

lab.cpp:18:6: error: no matching function for call to 'atoi'
i = atoi(line);
    ^~~~
役に立ちましたか?

解決

atoi expects const char*, not an std::string. So pass it one:

i = atoi(line.c_str());

Alternatively, use std::stoi:

i = std::stoi(line);

他のヒント

You have to use

const char *line = myString.c_str();

instead of:

std::string line = "Hello";

since atoi won't accept an std::string

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top