Why is it trying to send a char array instead of a string?
My function declaration is
siteObject(std::string& url, std::string& get, std::string& post);
So why is this site("String 1", "String 2", "String 3")
; creating a mismatch type error. 开发者_运维知识库It says it wants a string reference and it's receiving a char array. If you need more detail just ask in the comments.
Because there's an implicit call to the std::string
constructor, which creates a temporary object. You cannot take a non-const reference to a temporary (because it's meaningless to modify a temporary).
So, either modify your function to take const
references, or by-value, or pass it non-temporary objects.
Your siteObject
function:
siteObject(std::string& url, std::string& get, std::string& post);
takes non-const references to string objects, which cannot be bound to rvalues (or temporaries).
When you try to call the function with string literals, the compiler has to convert those arguments (which are char*
) to something that matches the parameter types - that conversion results in a temporary std::string
object.
You'll need to change your function to accept const references if you want to be able to bind them to temporaries:
siteObject(std::string const& url, std::string const& get, std::string const& post);
Or you could pass values instead of references:
siteObject(std::string url, std::string get, std::string post);
You need to either make your function accept const std::string& str
or construct string instances to pass in, and not rely on the implicit conversion of char* to string objects.
The correct call is:
std::string url("...");
std::string get("...");
std::string post("...");
siteObject(url, get, post);
This makes sense since the method signature implies that you get something back in the three strings (non-const references) and you may use those return values.
If that's not the intention and you have the ability to change the siteObject() method then you should do:
siteObject(std::string const & url, std::string const & get, std::string const & post);
and use your original call.
Strings entered in double quote characters are coming from the C heritage of C++ (they are called C string or NUL terminated string). They are implemented by the compiler as array of char
. On the contrary, the std::string
is a C++ class that aims to simplify manipulation of strings. It owns a C string (and can be created from one since it has a constructor that accept a const char*
), and manage its memory.
Since there exists a construtor of std::string
from const char*
and that C string are compatible with that type, how come the compiler cannot call this function ? This is because the function is taking non-const reference to std::string
objects. The constructor can't be used in this situation because the objects created would be temporaries, and you cannot get a non-const reference to a temporary object, as the called function may mutate it.
You can either create the std::string
and pass them to the function:
std::string s1("String 1");
std::string s2("String 2");
std::string s3("String 3");
site(s1, s2, s3);
Or you can change the prototype of the site
function to accept const reference. This is only possible if the function does not mutate the objects and you have access to the code:
// Declaration
void site(const std::string& s1, const std::string& s2, const std::string s3);
// Usage
site("String 1", "String 2", "String 3");
精彩评论