System()没有合适的转换C ++ [英] System() no suitable conversion C++
问题描述
我有两个文件名列表。
std::list<std::string> userfontlist;
std::list<std::string>::iterator it;
std::list<std::string> CAATfontlist;
std::list<std::string>::iterator it2;
std::list<std::string> doesnthavelist;
,我想比较2个列表,并删除它匹配的值。每个似乎都很好,直到我添加del / F / Q / A到y.c_string()。
and I want to compare the 2 lists, and delete the value it matches. every seems to be fine until I add the "del /F /Q /A" to the y.c_string().
for (it = userfontlist.begin(); it !=userfontlist.end(); ++it){
for(it2 = CAATfontlist.begin();it2 !=CAATfontlist.end();++it2){
if(*it==*it2){
string y = *it;
string k = y.c_str();
string a = "del /F /Q /A " + k;
system(a);
}
}
}
a)说我有没有合适的转换函数从std字符串到const char *存在。如果我把整个东西作为1字符串,它工作,但不是当我连接字符串和y.c_str()。我没有运气试图将std :: string转换为const * char,甚至不确定如果这甚至有帮助。
system(a) Says I have "no suitable conversion function from std string to const char * exists". If I put the whole thing as 1 string it works, but not when I concatenate the string and the y.c_str(). I have had no luck trying to convert the std::string to const *char, not even sure if that even helps.
推荐答案
正如它所说,没有理解 std :: string
和<$ c $的版本 system()
c> std :: string 不能自由转换为 const char *
(其中 system c $ c>确实理解),因为
std :: string
可能会在一般情况下没有意义的方式消失。
exactly as it says, there's no version of system()
that understands std::string
and std::string
can't be freely converted to const char *
(which system()
does understand), because std::string
may go away in ways that don't make sense in the general case.
您可以显式地从 std :: string
转换为 const char *
很容易,但是: std :: string :: c_str
,将最后一行写为
you can explicitly convert from std::string
to const char *
easily, though: with std::string::c_str
, write your last line as
system(a.c_str());
请注意返回的 const char *
的生命周期与 string
有关,一旦超出范围,指针就会变得无效。这个case是一个很好的时间使用它,虽然,因为 system()
不会保持指针超过自己的返回。
Be aware that the returned const char *
's lifetime is tied to the string
it came from, once it goes out of scope, the pointer becomes invalid. This case is a fine time to use it, though, since system()
won't "hold on" to the pointer past its own return.
这篇关于System()没有合适的转换C ++的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!