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)'
#include <iostream>
#include <string>
using namespace std;
int counter = 0;
void sillyFunction(string * str, int cool=0);
int main(){
sillyFunction("Cool");
sillyFun开发者_运维知识库ction("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;
}
}
Don't take your parameter in as a string *
try just using a const string &
instead
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.
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.
Also don't forget if you change from a string *
that you no longer need to dereference it in your cout
:
if (cool){
for (int i=0; i<counter; i++) cout << str << endl;
} else {
cout << str << endl;
}
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.
You have asked for a pointer to an existing std::string
object. By definition, what is passed to your function must be the address of an already existing std::string
(or descendant) object.
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 not a std::string
, nor can it be "converted" to a std::string
, nor can it be treated as a pointer-to-char (or vice-versa).
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.
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.5 in 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).
At the same time, if someone passes you an actual std::string
, the constructor is avoided and you get the same efficiency as if you had taken a pointer-to-std::string
. Win-win.
Alternatively, of course, you can just take a plain std::string
, but in that case you always get 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.
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;
}
}
You can convert from a const char *
to a string
, but not to a string *
.
Perhaps you want your sillyFunction
to take a const reference?
void sillyFunction(const string &str, int cool){
counter++;
if (cool){
for (int i=0; i<counter; i++) cout << str << endl;
} else {
cout << str << endl;
}
}
Should be
void sillyFunction(const string& str, int cool=0);
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.
int main(){
string str = "Cool";
string str2 = "Cooler";
sillyFunction(&str);
sillyFunction(&str2, 1);
return 0;
}
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.
I have a very simple solution for that, using string copy
char s[20] strcpy(s,const char *p);
Then you have the string pointed by *p in s... it works..
精彩评论