How do I fix a "no matching function for call to 'atoi'" error?

All indications tell me this is a ridiculously easy problem to solve, but I can't figure out error telling me the 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;
}


ошибка

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

2 ответа

Решение

atoi надеется const char* не std::string, Так что передайте это один:

i = atoi(line.c_str());

В качестве альтернативы используйте std::stoi:

i = std::stoi(line);

Вы должны использовать

const char *line = myString.c_str();

вместо:

std::string line = "Hello";

Атои не примет std::string

Другие вопросы по тегам