如何修复"没有匹配功能来调用'ati'"错误?

nip*_*ese 3 c++ atoi

所有迹象都告诉我这是一个非常容易解决的问题,但我无法弄清楚错误告诉我该atoi功能不存在.

C++

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

using namespace std;

string line;
int i;

int main() {

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

    return 0;
}
Run Code Online (Sandbox Code Playgroud)


错误

lab.cpp:18:6: error: no matching function for call to 'atoi'
i = atoi(line);
    ^~~~
Run Code Online (Sandbox Code Playgroud)

jua*_*nza 12

atoi期待const char*,而不是std::string.通过一个:

i = atoi(line.c_str());
Run Code Online (Sandbox Code Playgroud)

或者,使用std::stoi:

i = std::stoi(line);
Run Code Online (Sandbox Code Playgroud)