我想在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);
}
答案 0 :(得分:3)
假设您打算checkFilePath
成为会员,请将for_each
算法中的使用替换为:
[&](std::string const& s){return checkFilePath(s);}
它将捕获this
并调用该方法。以上需要C ++ 11,你可能没有。如果您有C ++ 03,则必须使用std::bind
或boost::bind
或编写自己的仿函数来捕获this
或解决checkFilePath
问题所需的状态。
如果您的checkFilePath
不依赖于this
的状态,只需创建方法static
,您的现有代码就应该编译。 (或者,使它成为一个自由函数)。
答案 1 :(得分:1)
函数checkFilePath是一个非静态成员函数。可以使用IO类型的对象调用它。因此,您可能不会简单地将其指定为for_each算法的第三个参数。 如果这个函数是静态成员函数会更简单。