这段代码有什么问题?

时间:2013-09-02 00:45:50

标签: c++ linux vector casting execvp

我试图用这些参数调用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*)’

尝试使用类型转换的所有排列,但是无法让它工作。 有什么问题?

1 个答案:

答案 0 :(得分:1)

变量vvector<char const*>语句中为execvp(subcommand[0].c_str(),v);,而evecvp的声明为int execvp(const char*, char* const*)