當我運行我的代碼,我得到在編譯時這個錯誤:無法轉換的字符/字符串爲int
# g++ -std=c++0x sixteen.cpp -O3 -Wall -g3 -o sixteen
sixteen.cpp: In function ‘int main()’:
sixteen.cpp:10: error: call of overloaded ‘stoi(char&)’ is ambiguous
/usr/lib/gcc/x86_64-redhat-linux/4.4.7/../../../../include/c++/4.4.7/bits/basic_string.h:2565: note: candidates are: int std::stoi(const std::string&, size_t*, int) <near match>
/usr/lib/gcc/x86_64-redhat-linux/4.4.7/../../../../include/c++/4.4.7/bits/basic_string.h:2626: note: int std::stoi(const std::wstring&, size_t*, int) <near match>
我擡頭的錯誤,隨後,關於這裏的其他問題也做了說明,但是我刪除using namespace std;
後仍然會出現該錯誤。爲什麼這仍在發生,我能做些什麼來擺脫它?
代碼:
#include <iostream>
#include <string>
int main() {
std::string test = "Hello, world!";
std::string one = "123";
std::cout << "The 3rd index of the string is: " << test[3] << std::endl;
int num = std::stoi(one[2]);
printf("The 3rd number is: %d\n", num);
return 0;
}
'std :: string'不能由單個'char'參數產生。 – chris
'int num = std :: stoi(&one.c_str()[2]);'會工作,但只是因爲它是C字符串中的最後一個非NULL字符。 –