문제

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