无法转换 'std::basic_string<char>'到 'const char*' 的参数 '1' 到 'int system(const char*)'

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*))

本文介绍了无法转换 'std::basic_string<char>'到 '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*)'

基础教程推荐