2014-05-10 78 views
3

所有的跡象告訴我這是一個可笑的簡單問題來解決,但我不知道錯誤告訴我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; 
} 


錯誤

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

您應該將'#include '更改爲正確的''並調用'std :: atoi'。 –

+0

請不要使用'atoi',除非你瘋了,而實際上更喜歡錯誤返回值是一個合法的結果。 – chris

+0

@MaxBozzi這是一個好主意,但它在這裏沒有幫助。 – juanchopanza

回答

11

atoi預計const char*,而不是一個std::string。所以它傳遞一個:

i = atoi(line.c_str()); 

另外,使用std::stoi

i = std::stoi(line); 
1

你必須使用

const char *line = myString.c_str(); 

代替:

std::string line = "Hello"; 

以來的atoi不會接受std::string

相關問題