简体   繁体   English

C ++从'char'到'const char *'的无效转换[-fpermissive]

[英]C++ invalid conversion from ‘char’ to ‘const char*’ [-fpermissive]

WARNING: Extremely limited knowledge of C++ and coding in general. 警告:通常,对C ++和编码的了解非常有限。 Please refrain from advanced terminology. 请避免使用高级术语。

for ( i = 0; i < answer.size(); ++i) {
            if (guess == answer.at(i)) {                            //to display correct letters in answerDisplay
                answerDisplay.replace( (2 * i), 1, answer.at(i) );
                correctGuesses += 1;
            }

Given: answerDisplay and answer are strings. 给定:answerDisplay和answer是字符串。

When I run my program there is a compile-time error at the third line of what I've posted saying: 当我运行程序时,在我发布的内容的第三行中有一个编译时错误:

invalid conversion from ‘char’ to ‘const char*’ [-fpermissive]

What's the problem? 有什么问题? How can I fix it? 我该如何解决? All other posts with this error talked about pointer characters but I don't know what those are. 所有其他出现此错误的帖子都谈到了指针字符,但我不知道它们是什么。

Pointer characters are they way plain strings are implemented in C and C++. 指针字符是在C和C ++中实现纯字符串的方式。 In C++ you have the nice class std::string , but string literals are still array of characters. 在C ++中,您拥有不错的类std::string ,但字符串文字仍然是字符数组。 And arrays in C and C++ can be seen as pointers. C和C ++中的数组可以看作是指针。

For example, "hello" is of type const char[6] (5 characters plus the ending NUL), but it can be trivially converted to const char * , and that in turn can be converted to std::string . 例如, "hello"的类型为const char[6] (5个字符加上结尾的NUL),但是可以将其简单地转换为const char * ,然后可以将其转换为std::string

In line 3, the only relevant code is a call to the member function std::string::replace() . 在第3行中,唯一相关的代码是对成员函数std::string::replace()的调用。 There are a lot of overrides of this function (different sets of parameters to be used), but the one the compiler is trying to use is this one: 此函数有很多替代(要使用的不同参数集),但是编译器尝试使用的是以下一个:

string& replace (size_t pos,  size_t len,  const char* s);

As you can see, it takes two numbers and a const char * (an old-string/char-array). 如您所见,它需要两个数字和一个const char * (旧字符串/字符数组)。 But you are passing as third parameter answer.at(i) that is of type char . 但是,您将传递作为char类型的第三个参数answer.at(i) Hence the error: 因此错误:

invalid conversion from ‘char’ to ‘const char*’

Solution? 解? You can build a string from that char: 您可以从该字符构建一个字符串:

answerDisplay.replace( (2 * i), 1, std::string(1, answer.at(i))

Or you can get a substring of the original string instead of a plain character. 或者,您可以获取原始字符串的子字符串,而不是普通字符。

answerDisplay.replace( (2 * i), 1, answer.substr(i, 1))

暂无
暂无

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

相关问题 (C++) 错误:从 'char' 到 'const char*' 的无效转换 [-fpermissive] - (C++) error: invalid conversion from ‘char’ to ‘const char*’ [-fpermissive] C ++无效转换为&#39;char&#39;到&#39;const char *&#39;[-fpermissive] | - C++ invalid conversion from 'char' to 'const char*' [-fpermissive]| C ++错误:从&#39;char&#39;到&#39;const char *&#39;的无效转换[-fpermissive] - C++ error: invalid conversion from ‘char’ to ‘const char*’ [-fpermissive] c++ 从“char”到“const char*”的无效转换 [-fpermissive] - c++ invalid conversion from 'char' to 'const char*' [-fpermissive] C ++错误:从&#39;int&#39;到&#39;const char *&#39;的无效转换[-fpermissive] | - C++ error: invalid conversion from 'int' to 'const char*' [-fpermissive]| 从&#39;char&#39;到&#39;const char *&#39;的无效转换[-fpermissive] - Invalid conversion from 'char' to 'const char*' [-fpermissive] 从&#39;const char *&#39;到&#39;char *&#39;的无效转换[-fpermissive] - invalid conversion from ‘const char*’ to ‘char*’ [-fpermissive] strcmp [c ++]错误:从&#39;char&#39;到&#39;const char *&#39;的无效转换[-fpermissive] - strcmp[c++] error: invalid conversion from ‘char’ to ‘const char*’ [-fpermissive] 从&#39;char&#39;到&#39;const char *&#39;的无效转换-fpermissive c ++ ASCII函数 - invalid conversion from 'char' to 'const char*' -fpermissive c++ ASCII function C++ atoi error 错误:从 'char' 到 'const char*' 的无效转换 [-fpermissive] - C++ atoi error error: invalid conversion from ‘char’ to ‘const char*’ [-fpermissive]
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM