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'");
The system function requires const char *, and your expression is of the type std::string
. You should write
string name = "john";
string system_str = " quickscan.exe resolution 300 selectscanner jpg showui showprogress filename '"+name+".jpg'";
system(system_str.c_str ());
try using concatenation of string
Statistics(string date)
{
this->date += date;
}
acually this was a part of a class..
std::string + const char*
results in another std::string
. system
does not take a std::string
, and you cannot concatenate char*
's with the +
operator. If you want to use the code this way you will need:
std::string name = "john";
std::string tmp =
"quickscan.exe resolution 300 selectscanner jpg showui showprogress filename '" +
name + ".jpg'";
system(tmp.c_str());
As all the other answers show, the problem is that adding a std::string
and a const char*
using +
results in a std::string
, while system()
expects a const char*
. And the solution is to use c_str()
. However, you can also do it without a temporary:
string name = "john";
system((" quickscan.exe resolution 300 selectscanner jpg showui showprogress filename '"+name+".jpg'").c_str());
The addition of a string literal with an std::string
yields another std::string
. system
expects a const char*
. You can use std::string::c_str()
for that:
string name = "john";
string tmp = " quickscan.exe resolution 300 selectscanner jpg showui showprogress filename '"+name+".jpg'"
system(tmp.c_str());
Source: Stackoverflow.com