cannot convert 'std::basic_string<char>' to 'const char*' for argument '1' to 'int system(const char*)'

144,679

Solution 1

The type of expression

" quickscan.exe resolution 300 selectscanner jpg showui showprogress filename '"+name+".jpg'"

is std::string. However function system has declaration

int system(const char *s);

that 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 type const char *.

Nevertheless class std::string has two functions that do this conversion explicitly. They are c_str() and data() (the last can be used only with compiler that supports C++11)

So you can write

string name = "john";

system( (" quickscan.exe resolution 300 selectscanner jpg showui showprogress filename '"+name+".jpg'").c_str() );

There is no need to use an intermediate variable for the expression.

Solution 2

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());

See std::string operator+(const char*)

Solution 3

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());

Solution 4

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());

Solution 5

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 ());
Share:
144,679
Giacomo Cerquone
Author by

Giacomo Cerquone

Hi, I'm Giacomo. I write and explain software to make the world a better place. Graduated in computer science, remote freelancer, JavaScript developer.

Updated on July 09, 2022

Comments

  • Giacomo Cerquone
    Giacomo Cerquone almost 2 years

    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'");
    
  • not2qubit
    not2qubit almost 3 years
    Well, then perhaps explain a bit how it works?