c++ptr-vector

Compile error in std::find_if


Can anyone explain what I am doing wrong here?

class Base_Type
{
public:     
    string name;    
    int localType;
};

boost::ptr_vector<Base_Type> tVector; 

struct findVariable
{
    findVariable(const string& name) : _name(name) {};
    const string& _name;

    bool operator () (const Base_Type& arg) const
    {
        return (_name == arg.name);
    }
};

typedef boost::ptr_vector<Base_Type>::iterator tVector_it; 

inline tVector_it findVariable(string& _name) 
{
    return find_if(tVector.begin(), tVector.end(), findVariable(_name));        
}

Compile Error:

...\vc\include\algorithm(43): error C2064: term does not evaluate to a function taking 1 arguments

...\vc\include\algorithm(54): note: see reference to function template instantiation '_InIt std::_Find_if<_Iter,_Pr>(_InIt,_InIt,_Pr)' being compiled with [ _InIt=boost::void_ptr_iterator>>,var_T::Base_Type>, _Iter=boost::void_ptr_iterator>>,var_T::Base_Type>, _Pr=var_T::tVector_it ]


Solution

  • You have a structure named findVariable, then you have a function named findVariable.

    In the function, when you do findVariable(_name) you don't create an instance of structure, you call the function recursively. And the function does not return something you can use as a predicate to std::find_if so the compiler gives you an error.

    Simple solution? Rename your structure, or your function.