I get this error: "invalid operands of types 'const char*' and 'const char [6]' to binary 'operator+'" when i try to compile my script. Here should be the error:
string name = "john";
system(" quickscan.exe resolution 300 selectscanner jpg showui showprogress filename '"+name+".jpg'");
std::string + const char*
results in anotherstd::string
.system
does not take astd::string
, and you cannot concatenatechar*
's with the+
operator. If you want to use the code this way you will need:See std::string operator+(const char*)
The system function requires const char *, and your expression is of the type
std::string
. You should writeThe type of expression
is
std::string
. However function system has declarationthat is it accepts an argumnet of type
const char *
There is no conversion operator that would convert implicitly an object of type
std::string
to object of typeconst char *
.Nevertheless class
std::string
has two functions that do this conversion explicitly. They arec_str()
anddata()
(the last can be used only with compiler that supports C++11)So you can write
There is no need to use an intermediate variable for the expression.
As all the other answers show, the problem is that adding a
std::string
and aconst char*
using+
results in astd::string
, whilesystem()
expects aconst char*
. And the solution is to usec_str()
. However, you can also do it without a temporary:The addition of a string literal with an
std::string
yields anotherstd::string
.system
expects aconst char*
. You can usestd::string::c_str()
for that: