std :: vector for_each错误C3867函数调用缺少参数列表

时间:2014-01-30 12:39:49

标签: c++ boost foreach stdvector

我想在for_each中使用std::vector<std::string>语法,但我一直收到以下错误:

Error   1   error C3867: 'IO::checkFilePath': 

function call missing argument list; use '&IO::checkFilePath' to create a pointer to member

这是我的代码:

void checkFilePath(const std::string& filepath);
void checkFileList(const std::vector<std::string>& filelist);

void IO::checkFilePath(const std::string& filepath)
{
    if (!boost::filesystem::exists(filepath) || !boost::filesystem::is_directory(filepath))
    {
        //do smth
    }

}

void IO::checkFileList(const std::vector<std::string>& filelist)
{
        std::for_each(filelist.begin(), filelist.end(), checkFilePath);
}

2 个答案:

答案 0 :(得分:3)

假设您打算checkFilePath成为会员,请将for_each算法中的使用替换为:

 [&](std::string const& s){return checkFilePath(s);}

它将捕获this并调用该方法。以上需要C ++ 11,你可能没有。如果您有C ++ 03,则必须使用std::bindboost::bind或编写自己的仿函数来捕获this或解决checkFilePath问题所需的状态。

如果您的checkFilePath不依赖于this的状态,只需创建方法static,您的现有代码就应该编译。 (或者,使它成为一个自由函数)。

答案 1 :(得分:1)

函数checkFilePath是一个非静态成员函数。可以使用IO类型的对象调用它。因此,您可能不会简单地将其指定为for_each算法的第三个参数。 如果这个函数是静态成员函数会更简单。