我试图用这些参数调用evecvp():
vector<string>subcommand;
void parse(char *str)
{
pid_t pid;
char *cmd1=(char *)malloc(sizeof(300));
cmd1=strtok(str," ");
while(cmd1!=NULL)
{
subcommand.push_back(cmd1);
cmd1=strtok(NULL," ");
}
subcommand.push_back('\0');
vector<char const*> v( subcommand.size() );
for( int i = 0; i < v.size(); ++i )
{
v[i] = subcommand[i].c_str();
}
fork();
if(pid==0)
execvp(subcommand[0].c_str(),v);
}
我收到此错误:
main1.cpp: In function ‘void parse(char*)’:
main1.cpp:80:34: error: cannot convert ‘std::vector<const char*>’ to ‘char* const*’ for argument ‘2’ to ‘int execvp(const char*, char* const*)’
尝试使用类型转换的所有排列,但是无法让它工作。 有什么问题?
答案 0 :(得分:1)
变量v
在vector<char const*>
语句中为execvp(subcommand[0].c_str(),v);
,而evecvp的声明为int execvp(const char*, char* const*)
。