简体   繁体   English

错误:无法转换 'std::__cxx11::basic_string<char> ::迭代器'</char>

[英]error: cannot convert ‘std::__cxx11::basic_string<char>::iterator’

I am writing a program to check if an input phrase is a palindrome.我正在编写一个程序来检查输入短语是否是回文。 Compiled using Xcode on MacBook Pro works fine.在 MacBook Pro 上使用 Xcode 编译可以正常工作。 Complied on zyBooks website returns an error.在 zyBooks 网站上编译返回错误。

How can update my code to address this issue?如何更新我的代码以解决此问题?

My code is:我的代码是:

#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;
}

The full error code from zyBooks is as follows: 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)
      |            ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~

The remove() function without including the algorithm.h library works to delete the existing files in the system.不包含algorithm.h库的remove() function 用于删除系统中的现有文件。 The function signature of this function looks like:此 function 的 function 签名如下所示:

int remove(const char *_Filename)

In the other hand, after using algorithm.h , you could now use the remove() for iterators:另一方面,在使用algorithm.h之后,您现在可以将remove()用于迭代器:

_FIter remove<_FIter, _Tp>(_ForwardIterator __first, _ForwardIterator __last, const _Tp &__value)

The problem was that there was no overload for the function where it accepts std::string object begin() and end() as arguments.问题是 function 没有过载,它接受std::string object begin()end()作为 arguments。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 OSX“错误:无法转换&#39;const std :: __ cxx11 :: basic_string”是什么意思 <char> “ 意思? - OSX What does “error: cannot convert 'const std::__cxx11::basic_string<char>” mean? 获取“错误:无法转换 'std::__cxx11::string* {aka std::__cxx11::basic_string<char> *}' 到 'const char*' 错误,同时将路径作为参数传递</char> - Getting "error: cannot convert 'std::__cxx11::string* {aka std::__cxx11::basic_string<char>*}' to 'const char*' error while passing a path as argument 尝试生成随机字母错误:无法转换 'std::__cxx11::string {aka std::__cxx11::basic_string<char> }' 到 'char' 在分配</char> - Trying to generate a random letter error: cannot convert 'std::__cxx11::string {aka std::__cxx11::basic_string<char>}' to 'char' in assignment 字符串未正确复制并显示错误:无法转换 'std::__cxx11::string {aka std::__cxx11::basic_string<char> }'</char> - string not copying correctly and showing error: cannot convert 'std::__cxx11::string {aka std::__cxx11::basic_string<char>}' 错误:无法从“向量”转换“标签” <std::vector<std::__cxx11::basic_string<char> &gt;&gt;' 到 ' 向量<std::__cxx11::basic_string<char> &gt;' </std::__cxx11::basic_string<char></std::vector<std::__cxx11::basic_string<char> - error: could not convert 'tab' from 'vector<std::vector<std::__cxx11::basic_string<char> >>' to 'vector<std::__cxx11::basic_string<char>>' (std::__cxx11::string) [T = std::__cxx11::basic_string<char> ; std::__cxx11::string = std::__cxx11::basic_string<char> ] 不能重载 - (std::__cxx11::string) [with T = std::__cxx11::basic_string<char>; std::__cxx11::string = std::__cxx11::basic_string<char>] cannot be overloaded 错误:为&#39;operator std::string {aka std::__cxx11::basic_string 指定的返回类型<char> }&#39; - Error:return type specified for 'operator std::string {aka std::__cxx11::basic_string<char>}' 错误:没有匹配的函数调用&#39;std :: map <std::__cxx11::basic_string<char> - error: no matching function for call to ‘std::map<std::__cxx11::basic_string<char> 错误:传递&#39;const字符串{aka const std :: __ cxx11 :: basic_string <char> }&#39;作为&#39;this&#39;参数 - Error: passing ‘const string {aka const std::__cxx11::basic_string<char>}’ as ‘this’ argument 错误:“operator+=”不匹配(操作数类型为“long double”和“std::__cxx11::basic_string”<char> &#39;) - error: no match for 'operator+=' (operand types are 'long double' and 'std::__cxx11::basic_string<char>')
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM