I have a method setValue(string& val) that I need to call for some default values.
I would like to call it like this:
setValue("");
setValue("random value");
but I get the following error with g++: no matching function to call setValue(const char [10])
any idea on how I can do this beside creating a temp string object ?
It works but I find it inconvenient:
string temp("random value");
setValue(temp);
The problem is that
string&will not bind to temporary objects, only to non const named objects (non-const lvalues in the Standard language). You’ll have to name an object to pass to your function, as you do with thetempvariable.One workaround is to use a
constreference to string and pass directly your string literal to it. This works because 1)stringhas a non-explicit constructor which acceptsconst char*2)const char [10]will decay toconst char*.