我在C++11中有这样的函数:
bool ccc(const string cc) {
vector<string> digits;
int aux;
for(int n = 0; n < cc.length(); ++n) {
digits.push_back(to_string(cc[n])); }
for(int s = 1; s < digits.size(); s += 2) {
aux = stoi(digits[s]);
aux *= 2;
digits[s] = to_string(aux);
aux = 0;
for(int f = 0; f < digits[s].length(); ++f) {
aux += stoi(digits[s][f]); }
digits[s] = to_string(aux);
aux = 0; }
for(int b = 0; b < digits.size(); ++b) {
aux += stoi(digits[b]); }
aux *= 9;
aux %= 10;
return (aux == 0); }
使用g++使用-std=c++11
标志进行编译时,我会得到这个错误:
crecarche.cpp: In function ‘bool ccc(std::string)’:
crecarche.cpp:18:12: error: no matching function for call to ‘stoi(__gnu_cxx::__alloc_traits<std::allocator<char>, char>::value_type&)’
18 | aux += stoi(digits[s][f]); }
| ~~~~^~~~~~~~~~~~~~
但是之后我使用了stoi
函数,没有发现该行有任何错误。
为什么编译器会抛给我这个错误,我如何修复它?
发布于 2022-02-27 14:55:43
错误消息告诉您,传递给stoi
的参数类型为
__gnu_cxx::__alloc_traits<std::allocator<char>, char>::value_type&
这是一种奇特的表达char&
的方式。之所以会出现这种情况,是因为digits[s]
已经是string&
类型,并且订阅它会给您提供一个char&
。
我不清楚你想做什么。也许您需要删除额外的下标,或者使用digits[s][f] - '0'
来计算数字值。C++要求十进制数字由后续的代码点表示,因此即使在理论实现中也是如此,这些实现不是基于Unicode的ISO646子集。
https://stackoverflow.com/questions/71285683
复制相似问题