Using find_if method in instance

I have an instance method that populates a row vector. I am trying to find a single vector record containing a specific substring (now this substring is fixed - simple).

I have .h:

namespace Data
{
namespace Shared
{

    class Logger
    {
      public:
        bool FindLogDirectoryPredicate(const string &str);
        int GetLogDirectory(string logConfigFile, string& logDirectory);
...
    }
}
}

and .cpp:

#include <algorithm>
#include <vector>
#include "Logger.h"

bool Logger::FindLogDirectoryPredicate(const string &str) 
{
    // Return false if string found.
    return str.find("File=") > 0 ? false : true;
}

int Logger::GetLogDirectory(string logConfigFile, string& logDirectory)
{
    vector<string> fileContents;
    ...
    vector<string>::iterator result = find_if(fileContents.begin(), fileContents.end(), FindLogDirectoryPredicate);
    ...
}

Compiling this in Visual Studio 2010, I get:

Error   7   error C3867: 'Data::Shared::Logger::FindLogDirectoryPredicate': function call missing argument list; use '&Data::Shared::Logger::FindLogDirectoryPredicate' to create a pointer to member   Logger.cpp  317 1   Portability

Throw and before the ref function in the call to find_if, you will get:

Error   7   error C2276: '&' : illegal operation on bound member function expression    Logger.cpp  317 1   Portability

I tried to put the predicate function outside the class, but that didn't seem to work - I could not find the error. I tried to qualify the predicate with the class name ..., which gave me another error in the algorithm (header):

Error   1   error C2064: term does not evaluate to a function taking 1 arguments    c:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\include\algorithm    83  1   Portability

The example that I followed from here indicates that it is relatively simple .... so what am I doing wrong?

+3
3

, FindLogDirectoryPredicate - : , - , . (this), .

find_if(fileContents.begin(), 
        fileContents.end(), 
        bind1st(mem_fun(&Logger::FindLogDirectoryPredicate), this));

?

mem_fun " - ". ( , ), operator() ( , !). , , -; Logger.

bind1st , ( , - const string &) , (const string &). bind1st (this).

, FindLogDirectoryPredicate static, , , .

+4

static

class Logger
{
  public:
    static bool FindLogDirectoryPredicate(const string &str);
}

, , .

result = std::find_if(begin(), end(), [&this] (const std::string& s) 
     { return FindLogDirectoryPredicate(s); } );

std:: mem_fun ( <functional>), ++ 98/++ 03

result = std::find_if(begin(), end(), 
     std::bind1st(std::mem_fun(&Logger::FindLogDirectoryPredicate), this) );
+3

.

static bool FindLogDirectoryPredicate(const string &str);
0

All Articles