Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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'"); 
like image 290
Giacomo Cerquone Avatar asked Feb 05 '14 21:02

Giacomo Cerquone


People also ask

How to convert std:: string to const char?

You can use the c_str() method of the string class to get a const char* with the string contents.

How do I convert a string to a char in C++?

The c_str() and strcpy() function in C++ C++ c_str() function along with C++ String strcpy() function can be used to convert a string to char array easily. The c_str() method represents the sequence of characters in an array of string followed by a null character ('\0'). It returns a null pointer to the string.


1 Answers

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.

like image 141
Vlad from Moscow Avatar answered Oct 13 '22 22:10

Vlad from Moscow