0

How can I index a boost::multi_index container using a member function of class(that is being stored in the multi_index) that returns a constant reference of another class?

The error I get is :

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

Edit1:

This is a complete verifiable piece of similar code I created which has the same error,

#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

codeworks
  • 149
  • 1
  • 15

1 Answers1

2

Use boost::multi_index::const_mem_fun.

Edited after OP's additional information: the return type specified in const_mem_fun has to be exactly the same as that of the function you want to use for indexing. Note the differences in your current code:

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

So, change the const_mem_fun part as follows:

const_mem_fun<StoreMe, const RetClass&, &StoreMe::getId> 
Joaquín M López Muñoz
  • 5,243
  • 1
  • 15
  • 20
  • Yes, thats what I am using already, this exactly is what I am using, – codeworks Oct 19 '16 at 02:02
  • My function is : virtual const RetClass& getId() const { return Id; } My usage is : boost::multi_index::hashed_non_unique > And thats when I get the error. – codeworks Oct 19 '16 at 02:05
  • Created a verifiable sample that I added to the question @Joaquín M López Muñoz. TIA! – codeworks Oct 19 '16 at 04:35
  • Also @Joaquin, if I have an hashed_unique index by mistake and I had duplicates added, would my value be replaced and only the last value remains? – codeworks Oct 19 '16 at 09:10
  • Also I understand from the documentation that the speed of insertion is O(1) and look up is also O(1) when using hashed_unique. Am I right? What is in the case of hashed_non_unique? I mean the average case. – codeworks Oct 19 '16 at 09:11
  • The exact information can be found at the reference: http://www.boost.org/libs/multi_index/doc/reference/hash_indices.html#complexity_signature http://www.boost.org/libs/multi_index/doc/reference/hash_indices.html#modifiers http://www.boost.org/libs/multi_index/doc/reference/hash_indices.html#lookup – Joaquín M López Muñoz Oct 19 '16 at 10:06
  • Thanks Joaquin! That helped! In my problem, I am able to create a multi_index container and while inserting the compiler complains that, ...functional\hash\extensions.hpp(262): error C2664: 'size_t boost::hash_value(std::type_index)' : cannot convert argument 1 from 'const RetClass' to 'std::type_index' Do I need to create a comparision function or something? And if so how do I let it know that this is the function to use? – codeworks Oct 19 '16 at 14:08
  • I solved the issue by doing this. Can you @Joaquin please let me know if its right? The function I want to use returns a class which is a class that a wrapper around a string with a LOT of functionalities, so it looked like `virtual const RetClass& getId() const { return parentclass.Id; }` I wrote another function like, `virtual std::string getIdString() const { return string((parentclass.Id).tocharptr()); }` NOTE: tocharptr() is a method of the RetClass that will return a char* of a RetClass – codeworks Oct 19 '16 at 14:31