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*)'
基础教程推荐
- 初始化列表*参数*评估顺序 2021-01-01
- 通过引用传递 C++ 迭代器有什么问题? 2022-01-01
- 为什么 typeid.name() 使用 GCC 返回奇怪的字符以及如 2022-09-16
- GDB 显示调用堆栈上函数地址的当前编译二进制文 2022-09-05
- 非静态 const 成员,不能使用默认赋值运算符 2022-10-09
- 我应该对 C++ 中的成员变量和函数参数使用相同的名称吗? 2021-01-01
- CString 到 char* 2021-01-01
- 为什么 RegOpenKeyEx() 在 Vista 64 位上返回错误代码 2021-01-01
- 为什么派生模板类不能访问基模板类的标识符? 2021-01-01
- 如果我为无符号变量分配负值会发生什么? 2022-01-01
