C++,从“char”到“const char*”的无效转换

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7732798/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 17:24:45  来源:igfitidea点击:

C++, Invalid conversion from ‘char’ to ‘const char*’

c++stringpointers

提问by fuddin

In C++ I have this program:

在 C++ 中,我有这个程序:

#include <iostream>
using namespace std;
int main(){
    string expression_input;
    cout << "User Input: " << endl;
    getline(cin, expression_input);
    expression_input.insert(0, '(');   //error happens here.
    expression_input.append(')');
}

I am getting the following error:

我收到以下错误:

prog.cpp:15: error: invalid conversion from ‘char' to ‘const char*'

prog.cpp:15: error:   initializing argument 2 of 
‘std::basic_string<_CharT, _Traits, _Alloc>& std::basic_string<_CharT, 
_Traits, _Alloc>::insert(typename _Alloc::rebind<_CharT>::other::size_type, 
const _CharT*) [with _CharT = char, _Traits = std::char_traits<char>, 
_Alloc = std::allocator<char>]'

Where am I am converting from charto const char*? Can't I insert a character at position 0 of a string?

我从哪里转换charconst char*?我不能在字符串的位置 0 插入一个字符吗?

回答by Paul R

The error message tells you everything you need to know - you're trying to pass a charparameter where a const char *is required.

错误消息告诉您需要知道的一切 - 您试图charconst char *需要a 的地方传递参数。

You just need to change:

你只需要改变:

expression_input.insert(0,'(');
expression_input.append(')');

to:

到:

expression_input.insert(0,"(");
expression_input.append(")");

回答by Nicol Bolas

There is no std::string::insertthat takes only a position and a character. What you want is insert(0, 1, '('), which inserts 1 character at position 0.

没有std::string::insert只需要一个位置和一个字符的。你想要的是insert(0, 1, '('),它在位置 0 插入 1 个字符。

The same goes for std::string::append: append(1, ')')

这同样适用于std::string::appendappend(1, ')')

回答by Marlon

Now I don't get why does the compiler says I am converting from char to const char*.

现在我不明白为什么编译器说我正在从 char 转换为 const char*。

The reason you are getting a compilation error is because there is no matching overload for the set of arguments you are passing to the method. The compiler tries to find the closest match, which in your case charand const char*, and then reports that error.

您收到编译错误的原因是因为您传递给该方法的参数集没有匹配的重载。编译器尝试找到最接近的匹配项,在您的情况下为charconst char*,然后报告该错误。

Please help me out.

请帮帮我。

There are 8 overloads for std::string::insertand 6 overloads for std::string::append. You have many different options such as:

std::string::insert有 8个重载,std::string::append 有6 个重载。您有许多不同的选择,例如:

expression_input.insert(0, "(");
expression_input.append(")");

or

或者

expression_input.insert(expression_input.begin(), '(');
expression_input.append(")");

or even

甚至

expression_input.insert(0, 1, '(');
expression_input.append(")");

There's many possibilities, just choose one that you find most readable or sutiable for your situation.

有很多可能性,只需选择一种您认为最易读或最适合您的情况的可能性。

回答by minus

the version of string::insert function you are using requires const char* you are putting in char use " instead of '

您正在使用的 string::insert 函数的版本需要 const char* 您在 char 中使用 " 而不是 '

here is a link to method documentation

这是方法文档的链接

http://www.cplusplus.com/reference/string/string/insert/

http://www.cplusplus.com/reference/string/string/insert/