![](/img/trans.png)
[英]OSX What does “error: cannot convert 'const std::__cxx11::basic_string<char>” mean?
[英]error: cannot convert ‘std::__cxx11::basic_string<char>::iterator’
我正在编写一个程序来检查输入短语是否是回文。 在 MacBook Pro 上使用 Xcode 编译可以正常工作。 在 zyBooks 网站上编译返回错误。
如何更新我的代码以解决此问题?
我的代码是:
#include <iostream>
#include <string>
#include <cctype>
using namespace std;
int main()
{
string userInput, palindromeCheck, reversedInput;
getline(cin, userInput);
palindromeCheck = userInput;
remove(palindromeCheck.begin(), palindromeCheck.end(), ' ');
palindromeCheck.erase(palindromeCheck.size() - 3, palindromeCheck.size());
int temp = 1;
while (temp < palindromeCheck.size() + 1)
{
reversedInput += palindromeCheck.at(palindromeCheck.size() - temp);
++temp;
}
if (reversedInput == palindromeCheck)
{
cout << userInput << " is a palindrome";
}
else
{
cout << userInput << " is not a palindrome";
}
cout << endl;
return 0;
}
zyBooks 的完整错误代码如下:
main.cpp: In function ‘int main()’:
main.cpp:14:33: error: cannot convert ‘std::__cxx11::basic_string<char>::iterator’ {aka ‘__gnu_cxx::__normal_iterator<char*, std::__cxx11::basic_string<char> >’} to ‘const char*’
14 | remove(palindromeCheck.begin(), palindromeCheck.end(), ' ');
| ~~~~~~~~~~~~~~~~~~~~~^~
| |
| std::__cxx11::basic_string<char>::iterator {aka __gnu_cxx::__normal_iterator<char*, std::__cxx11::basic_string<char> >}
In file included from /usr/include/c++/9/cstdio:42,
from /usr/include/c++/9/ext/string_conversions.h:43,
from /usr/include/c++/9/bits/basic_string.h:6493,
from /usr/include/c++/9/string:55,
from /usr/include/c++/9/bits/locale_classes.h:40,
from /usr/include/c++/9/bits/ios_base.h:41,
from /usr/include/c++/9/ios:42,
from /usr/include/c++/9/ostream:38,
from /usr/include/c++/9/iostream:39,
from main.cpp:1:
/usr/include/stdio.h:178:32: note: initializing argument 1 of ‘int remove(const char*)’
178 | extern int remove (const char *__filename) __THROW;
| ~~~~~~~~~~~~^~~~~~~~~~
main.cpp:20:17: warning: comparison of integer expressions of different signedness: ‘int’ and ‘std::__cxx11::basic_string<char>::size_type’ {aka ‘long unsigned int’} [-Wsign-compare]
20 | while (temp < palindromeCheck.size() + 1)
| ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~
不包含algorithm.h
库的remove()
function 用于删除系统中的现有文件。 此 function 的 function 签名如下所示:
int remove(const char *_Filename)
另一方面,在使用algorithm.h
之后,您现在可以将remove()
用于迭代器:
_FIter remove<_FIter, _Tp>(_ForwardIterator __first, _ForwardIterator __last, const _Tp &__value)
问题是 function 没有过载,它接受std::string
object begin()
和end()
作为 arguments。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.