当我运行我的代码时,编译时出现了这个错误:
# 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;
}
char
参数创建std::string
。 - chrisint num = std::stoi (&one.c_str ()[2]);
可以工作,但这仅因为它是 C 字符串中最后一个非 NULL 字符。 - Andon M. Coleman