cannot convert #39;std::basic_stringlt;chargt;#39; to #39;const char*#39; for argument #39;1#39; to #39;int system(const char*)#39;(无法转换 std::basic_stringlt;chargt;到 const char* 的参数 1 到 int system(const char*))
问题描述
当我尝试编译我的脚本时,我收到此错误:'const char*' 和 'const char [6]' 类型的无效操作数到二进制 'operator+'".这里应该是错误:
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'");
推荐答案
表达式的类型
" quickscan.exe resolution 300 selectscanner jpg showui showprogress filename '"+name+".jpg'"
是 std::string
.但是函数系统有声明
is std::string
. However function system has declaration
int system(const char *s);
也就是说,它接受 const char *
没有转换运算符可以将 std::string
类型的对象隐式转换为 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
有两个函数可以显式地进行这种转换.它们是c_str()
和data()
(最后一个只能用于支持C++11的编译器)
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)
所以你可以写
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.
这篇关于无法转换 'std::basic_string<char>'到 'const char*' 的参数 '1' 到 'int system(const char*)'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:无法转换 'std::basic_string<char>'到 'const char*' 的参数 '1' 到 'int system(const char*)'


基础教程推荐
- 如何检查GTK+3.0中的小部件类型? 2022-11-30
- 常量变量在标题中不起作用 2021-01-01
- 静态库、静态链接动态库和动态链接动态库的 .lib 文件里面是什么? 2021-01-01
- 如何在 C++ 中初始化静态常量成员? 2022-01-01
- 在 C++ 中计算滚动/移动平均值 2021-01-01
- C++结构和函数声明。为什么它不能编译? 2022-11-07
- 如何通过C程序打开命令提示符Cmd 2022-12-09
- 如何将 std::pair 的排序 std::list 转换为 std::map 2022-01-01
- 我有静态或动态 boost 库吗? 2021-01-01
- 这个宏可以转换成函数吗? 2022-01-01