C++ 无法将参数 '1' 的 'std::basic_string<char>' 转换为 'const char*' 到 'int system(const char*)'
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21589353/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
cannot convert 'std::basic_string<char>' to 'const char*' for argument '1' to 'int system(const char*)'
提问by Giacomo Cerquone
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:
当我尝试编译我的脚本时,我收到此错误:“类型为 'const char*' 和 'const char [6]' 的无效操作数到二进制 'operator+'”。这里应该是错误:
string name = "john";
system(" quickscan.exe resolution 300 selectscanner jpg showui showprogress filename '"+name+".jpg'");
回答by Vlad from Moscow
The type of expression
表达的类型
" quickscan.exe resolution 300 selectscanner jpg showui showprogress filename '"+name+".jpg'"
is std::string
. However function system has declaration
是std::string
。但是函数系统有声明
int system(const char *s);
that is it accepts an argumnet of type const char *
也就是说它接受一个类型的参数 const char *
There is no conversion operator that would convert implicitly an object of type std::string
to object of type const char *
.
没有,这样会隐含类型的对象转换转换操作符std::string
,以类型的对象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)
然而 classstd::string
有两个函数可以显式地进行这种转换。它们是c_str()
和data()
(最后一个只能与支持 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.
不需要为表达式使用中间变量。
回答by Ed S.
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 + const char*
结果在另一个std::string
. system
不接受 a std::string
,并且您不能将char*
's 与+
运算符连接起来。如果您想以这种方式使用代码,您将需要:
std::string name = "john";
std::string tmp =
"quickscan.exe resolution 300 selectscanner jpg showui showprogress filename '" +
name + ".jpg'";
system(tmp.c_str());
回答by juanchopanza
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:
将字符串文字与std::string
相加会产生另一个std::string
. system
期待一个const char*
. 你可以使用std::string::c_str()
:
string name = "john";
string tmp = " quickscan.exe resolution 300 selectscanner jpg showui showprogress filename '"+name+".jpg'"
system(tmp.c_str());
回答by Angew is no longer proud of SO
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:
正如所有其他答案所示,问题是添加 astd::string
和 a const char*
using+
结果是 a std::string
,而system()
期望 a const char*
。解决方案是使用c_str()
. 但是,您也可以在没有临时文件的情况下执行此操作:
string name = "john";
system((" quickscan.exe resolution 300 selectscanner jpg showui showprogress filename '"+name+".jpg'").c_str());
回答by Alex Telishev
The system function requires const char *, and your expression is of the type std::string
. You should write
system 函数需要 const char *,并且您的表达式的类型为std::string
。你应该写
string name = "john";
string system_str = " quickscan.exe resolution 300 selectscanner jpg showui showprogress filename '"+name+".jpg'";
system(system_str.c_str ());