【问题标题】:Index with boost multi_index带 bo​​ost multi_index 的索引
【发布时间】:2016-10-18 18:04:08
【问题描述】:

如何使用返回另一个类的常量引用的类的成员函数(存储在 multi_index 中)索引 boost::multi_index 容器?

我得到的错误是:

error C2440: 'specialization' : cannot convert from 'overloaded-function' to 'RetClass (__thiscall StoreMe::* )(void) const'

编辑1:

这是我创建的一段完整的可验证的类似代码,它具有相同的错误,

#include "stdafx.h"
#include<multi_index_container.hpp>
#include<boost/multi_index/hashed_index.hpp>
#include<boost/multi_index/mem_fun.hpp>


class RetClass
{
    int a, b;

};

class StoreMe
 {
    RetClass ex;
public:
    void setId(RetClass a) {
        ex = a;
    };


    virtual const RetClass& getId() const { return ex; }

};

typedef boost::multi_index_container<
    StoreMe,
    boost::multi_index::indexed_by<
        boost::multi_index::hashed_non_unique<boost::multi_index::const_mem_fun<StoreMe,     RetClass, &StoreMe::getId> >
    >
> mi_storeMe;

int _tmain(int argc, _TCHAR* argv[])
{
    return 0;
}

TIA

-R

【问题讨论】:

    标签: boost multi-index boost-multi-index


    【解决方案1】:

    使用boost::multi_index::const_mem_fun

    在 OP 的附加信息后编辑:const_mem_fun 中指定的返回类型必须与您要用于索引的函数的类型完全相同。请注意当前代码中的差异:

    virtual const RetClass& getId() const;
    const_mem_fun<StoreMe, RetClass, &StoreMe::getId>
    

    因此,将const_mem_fun 部分更改如下:

    const_mem_fun<StoreMe, const RetClass&, &StoreMe::getId> 
    

    【讨论】:

    • 是的,这就是我已经在使用的,这正是我正在使用的,
    • 我的函数是: virtual const RetClass& getId() const { return Id;我的用法是: boost::multi_index::hashed_non_unique<:multi_index::const_mem_fun retclass> > 那是我得到错误的时候。
    • 创建了一个可验证的样本,我将其添加到@Joaquín M López Muñoz 的问题中。 TIA!
    • 另外@Joaquin,如果我错误地添加了 hashed_unique 索引并且添加了重复项,我的值会被替换并且只剩下最后一个值吗?
    • 另外我从文档中了解到,使用 hashed_unique 时插入速度是 O(1),查找也是 O(1)。我对吗? hashed_non_unique 的情况是什么?我的意思是平均情况。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-06-15
    • 2014-05-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-05-20
    相关资源
    最近更新 更多