【问题标题】:std::find and boost::make_indirect_iterator - compilation errorstd::find 和 boost::make_indirect_iterator - 编译错误
【发布时间】:2014-05-23 22:24:03
【问题描述】:

我有问题。为什么我不能编译这个?怎么了?

#include <boost/iterator/indirect_iterator.hpp>

bool finder(std::list<SomeObject*>::const_iterator first, 
        std::list<SomeObject*>::const_iterator last, 
        const SomeObject& x) 
{
   return std::find(boost::make_indirect_iterator(first),
                    boost::make_indirect_iterator(last),
                    x) != boost::make_indirect_iterator(last); 
}

//此代码来自我之前帖子的回答

我有如下错误:

C:\Program Files (x86)\Microsoft Visual Studio 8\VC\include\algorithm(40) : 错误 C2784: 'bool std::operator ==(const _Ty &,const std::complex<_other> &)' : 无法从 'const SomeObject

推导出 'const std::complex<_other> &' 的模板参数

C:\Program Files (x86)\Microsoft Visual Studio 8\VC\include\algorithm(40) : 错误 C2784: 'bool std::operator ==(const std::complex<_other> &,const _Ty &)' : 无法推断模板 来自 'SomeObject' 的 'const std::complex<_other> &' 的参数

C:\Program Files (x86)\Microsoft Visual Studio 8\VC\include\algorithm(40) : 错误 C2784: 'bool std::operator ==(const std::pair<_ty1> &,const std::pair<_ty1> &)':不能 从 'const std::pair<_ty1> &' 推导出模板参数 一些对象

还有几个类似的错误..

我已经定义了operator==:

bool operator==(const SomeObject& x, const SomeObject& y)
{
    return x.id1() == y.id1();
}

我使用 VS 2005。

如何解决?怎么了?也许是VS2005的错误?你能编译这个吗?

【问题讨论】:

    标签: c++ algorithm boost stl visual-studio-2005


    【解决方案1】:

    这意味着 *advance(boost::make_indirect_iterator(first), some_int) 不能传递给接受 const SomeObject& 的 operator ==。这很奇怪。

    一个建议是将 bool operator== 保留在命名空间 std 内。

    namespace std {
      bool operator==(const SomeObject& x, const SomeObject& y)
      {
        return x.id1() == y.id1();
      }
    }
    

    【讨论】:

    • 我应该如何定义 operator== ?
    • 您可以尝试将 operator== 移动到命名空间 std 中吗?
    • 非常感谢。真的行!但这对我来说很奇怪......有谁知道如何做得更好?最好的?
    • 编译器试图在 std 命名空间中找到比较运算符的匹配项,但不幸的是匹配的比较运算符的通用模板化版本。砰!!!关于如何做得更好,我建议您将比较函数作为第四个参数传递给 std::find。
    • 我很抱歉我之前的陈述,虽然从未使用过,但我的印象是 find 将比较函数作为可选的第四个参数。请忽略我的评论。我会想一个更干净的方式和帖子。
    猜你喜欢
    • 2012-05-31
    • 1970-01-01
    • 2012-12-09
    • 2012-04-29
    • 2012-01-22
    • 1970-01-01
    • 1970-01-01
    • 2016-11-20
    • 2014-02-26
    相关资源
    最近更新 更多