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

前端 未结 6 1264
无人及你
无人及你 2020-11-29 07:18

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:

6条回答
  •  栀梦
    栀梦 (楼主)
    2020-11-29 08:05

    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.

提交回复
热议问题