C++ 无法将“const char*”转换为“std::string*”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5992953/
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
C++ cannot convert 'const char*' to 'std::string*'
提问by Pwnna
I have this code below and I'm getting the error upon compilation:
我在下面有这个代码,编译时出现错误:
error: cannot convert 'const char*' to 'std::string*' for argument '1' to 'void sillyFunction(std::string*, int)'
error: cannot convert 'const char*' to 'std::string*' for argument '1' to 'void sillyFunction(std::string*, int)'
#include <iostream>
#include <string>
using namespace std;
int counter = 0;
void sillyFunction(string * str, int cool=0);
int main(){
sillyFunction("Cool");
sillyFunction("Cooler", 1);
return 0;
}
void sillyFunction(string * str, int cool){
counter++;
if (cool){
for (int i=0; i<counter; i++) cout << *str << endl;
} else {
cout << *str << endl;
}
}
回答by Dan F
Don't take your parameter in as a string *
try just using a const string &
instead
不要string *
尝试使用您的参数,const string &
而只是使用 a代替
EDIT:
编辑:
std::string
and const char*
are different types. the std::string
already has a conversion from string literals (ex: "Cool"
) to the actual string object. So by passing in the string literal "Cool"
you are in a sense passing in a std::string
object, not a pointer to one.
std::string
并且const char*
是不同的类型。在std::string
已经具有从字符串文字的转化:(购"Cool"
到的实际字符串对象)。因此,通过传入字符串文字,"Cool"
您在某种意义上是传入一个std::string
对象,而不是一个指向对象的指针。
The reason I chose to use a const string &
is mostly from personal coding practices. This minimizes stack memory usage, and since you are passing in a constant string literal, there is no need for the parameter to be modify-able.
我选择使用 a 的原因const string &
主要来自个人编码实践。这最大限度地减少了堆栈内存使用量,并且由于您传入的是常量字符串文字,因此不需要可修改参数。
Also don't forget if you change from a string *
that you no longer need to dereference it in your cout
:
另外不要忘记,如果您从 a 更改string *
,您不再需要在您的cout
:
if (cool){
for (int i=0; i<counter; i++) cout << str << endl;
} else {
cout << str << endl;
}
回答by Sam Miller
change
改变
void sillyFunction(string * str, int cool){
counter++;
if (cool){
for (int i=0; i<counter; i++) cout << *str << endl;
} else {
cout << *str << endl;
}
}
to
到
void sillyFunction(const char* str, int cool){
counter++;
if (cool){
for (int i=0; i<counter; i++) cout << str << endl;
} else {
cout << str << endl;
}
}
回答by Nicholas Knight
To explain what the problem actually is...
为了解释问题究竟是什么......
While the compiler will happily arrange for a char *
/C-string to be "converted" to a std::string
via the appropriate std::string
constructor, that's not what you've asked for.
虽然编译器很乐意通过适当的构造函数将char *
/C 字符串“转换”为,但这不是您所要求的。std::string
std::string
You have asked for a pointerto an existingstd::string
object. By definition, what is passed to your function must be the address of an already existingstd::string
(or descendant) object.
你问了一个指针到一个现有的std::string
对象。根据定义,传递给函数的内容必须是已存在std::string
(或后代)对象的地址。
You must understand pointers as a distinct type -- your function takes a pointer-to-std::string
"object". While a std::string
can be accessed via a pointer to std::string
, the pointer itself is nota std::string
, nor can it be "converted" to a std::string
, nor can it be treated as a pointer-to-char (or vice-versa).
您必须将指针理解为一种独特的类型——您的函数需要一个pointer-to-std::string
“对象”。虽然 astd::string
可以通过指向 的指针访问std::string
,但指针本身不是a std::string
,也不能“转换”为 a std::string
,也不能将其视为指向字符的指针(反之亦然)。
The easiest alternative is indeed a const reference to a std::string
(const std::string &
). const, in this case, because you're not doing anything to modify the string. If you were, that would be a different matter, and you'd have to consider carefully whether you intend for the caller to see your changes.
最简单的替代方法确实是对 a std::string
( const std::string &
)的 const 引用。const,在这种情况下,因为您没有做任何事情来修改字符串。如果是,那将是另一回事,您必须仔细考虑您是否打算让呼叫者看到您的更改。
By doing this, you're saying you want a std::string
object (remember, a reference to an object is that object, see C++ FAQ 8.5in particular), which allows the compiler to invoke the appropriate constructor to create a std::string for you when the function is called with a char *
(const or not).
通过这样做,你是说你想要一个std::string
对象(记住,对对象的引用就是那个对象,特别是参见C++ FAQ 8.5),它允许编译器调用适当的构造函数来为你创建一个 std::string当使用char *
(const 或 not)调用函数时。
At the same time, if someone passes you an actualstd::string
, the constructor is avoided and you get the same efficiency as if you had taken a pointer-to-std::string
. Win-win.
同时,如果有人传递给您一个实际的std::string
,则避免使用构造函数,您将获得与使用pointer-to-std::string
. 双赢。
Alternatively, of course, you can just take a plain std::string
, but in that case you alwaysget a copy of the string being passed in, whether it's a C-string or a std::string
. Sometimes that's desirable, sometimes not. In your case, you don't do anything but print the string out, making the overhead unnecessary.
或者,当然,您可以只使用普通的std::string
,但在这种情况下,您始终会获得传入的字符串的副本,无论它是 C 字符串还是std::string
. 有时这是可取的,有时不是。在您的情况下,除了打印字符串外,您什么都不做,从而无需额外开销。
回答by detunized
You can acheive that by changing the prototype to:
您可以通过将原型更改为:
void sillyFunction(string const &str, int cool=0);
char const *
could be implicitly converted to a temporary std::string
, which in turn could be passed by reference (std::string const &
). There's no implicit conversion to a pointer (std::string *
), that's why you get the error.
char const *
可以隐式转换为临时的std::string
,而临时的又可以通过引用 ( std::string const &
)传递。没有隐式转换为指针 ( std::string *
),这就是您收到错误的原因。
回答by Nick Meyer
You can convert from a const char *
to a string
, but not to a string *
.
您可以从 a 转换const char *
为 a string
,但不能转换为 a string *
。
Perhaps you want your sillyFunction
to take a const reference?
也许您希望sillyFunction
采用 const 引用?
void sillyFunction(const string &str, int cool){
counter++;
if (cool){
for (int i=0; i<counter; i++) cout << str << endl;
} else {
cout << str << endl;
}
}
回答by Marius Bancila
Should be
应该
void sillyFunction(const string& str, int cool=0);
回答by richmb
If you're going to use pointers for your functions you have to declare the strings somewhere. memory needs to be allocated. so either declare a variable or call new to create memory for the string.
如果您要为函数使用指针,则必须在某处声明字符串。需要分配内存。所以要么声明一个变量,要么调用 new 来为字符串创建内存。
int main(){
string str = "Cool";
string str2 = "Cooler";
sillyFunction(&str);
sillyFunction(&str2, 1);
return 0;
}
回答by dibin_salher
I have a very simple solution for that, using string copy
我有一个非常简单的解决方案,使用字符串复制
char s[20] strcpy(s,const char *p);
char s[20] strcpy(s,const char *p);
Then you have the string pointed by *p in s... it works..
然后你在 s 中得到了 *p 指向的字符串......它起作用了..