I want to use the for_each syntax with std::vector<std::string> but I keep getting the following error:

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

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

Here is my code:

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);
}
有帮助吗?

解决方案

Assuming you intend for checkFilePath to be a member, replace the use of it in the for_each algorithm with:

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

and it will capture this and call the method. The above requires C++11, which you may not have. If you have C++03, you either have to use std::bind or boost::bind or write your own functor to capture this or the state required to solve the checkFilePath problem.

If your checkFilePath does not depend on the state of this, simply make the method static and your existing code should compile. (Alternatively, make it a free function).

其他提示

Function checkFilePath is a non-static member function. It can be called using an object of type IO. So you may not simply specify it as the third parameter of the for_each algorithm. It would be simpler if this function would be a static member function.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top