c++ - std::find_if 中的编译错误

标签 c++ ptr-vector

<分区>

谁能解释一下我在这里做错了什么?

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));        
}

编译错误:

...\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 ]

最佳答案

您有一个名为 findVariable 的结构,然后您有一个名为 findVariable 的函数。

在函数中,当您执行 findVariable(_name) 时,您不会创建结构实例,而是递归调用函数。而且该函数不会返回可用作 std::find_if 谓词的内容,因此编译器会报错。

简单的解决方案?重命名您的结构或函数。

关于c++ - std::find_if 中的编译错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34980971/

相关文章:

C++ 错误 : array initializer must be an initializer list

c++ - 在这种情况下如何使用 NULL?

c++ - 单应性投影矩阵

c++ - ptr_vector - _CrtDumpMemoryLeaks() - 即使调用析构函数也会发生内存泄漏

c++ - 发布 boost::ptr_vector,不匹配文档

c++ - 如何确定 boost::variant 变量是否为空?

c++ - 使用assert帮助编译器更好的优化

c++ - boost::ptr_vector 和 boost::any 的问题

c++ - 为 boost::ptr_vector 创建一个容器类

c++ - 错误 : invalid use of 'Config::testMap'