std::string mail;
std::string password;
std::system("mkdir ~/.cache/pbt");
std::cout << "Enter your accounts mail" << std::endl;
std::cin >> mail;
std::cout << "Now enter your accounts password";
std::cin >> password;
std::system("perl zap2xml.pl -u " + mail + " -p " + password + " -o ~/.cache/pbt");
这就是编译器所说的
main.cpp:13:62: error: cannot convert ‘std::__cxx11::basic_string<char>’ to ‘const char*’
13 | std::system("perl zap2xml.pl -u " + mail + " -p " + password + " -o ~/.cache/pbt");
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~
| |
| std::__cxx11::basic_string<char>
In file included from /usr/include/c++/9.2.0/cstdlib:75,
from /usr/include/c++/9.2.0/ext/string_conversions.h:41,
from /usr/include/c++/9.2.0/bits/basic_string.h:6493,
from /usr/include/c++/9.2.0/string:55,
from /usr/include/c++/9.2.0/bits/locale_classes.h:40,
from /usr/include/c++/9.2.0/bits/ios_base.h:41,
from /usr/include/c++/9.2.0/ios:42,
from /usr/include/c++/9.2.0/ostream:38,
from /usr/include/c++/9.2.0/iostream:39,
from main.cpp:1:
/usr/include/stdlib.h:784:32: note: initializing argument 1 of ‘int system(const char*)’
784 | extern int system (const char *__command) __wur;
| ~~~~~~~~~~~~^~~~~~~~~
在我看来,好像std :: system无法读取字符串变量,但是必须有某种方法可以实现
答案 0 :(得分:2)
std::system()
将const char*
作为输入,但是您正尝试将其传递给std::string
。 std::string
不能隐式转换为const char*
,因此会导致编译器错误。但是,您可以使用字符串的c_str()
方法来获取const char*
,例如:
std::string cmd = "perl zap2xml.pl -u " + mail + " -p " + password + " -o ~/.cache/pbt";
std::system(cmd.c_str());
或者:
std::system(("perl zap2xml.pl -u " + mail + " -p " + password + " -o ~/.cache/pbt").c_str());