Question

I have a very simple multi index container that indexes members of a class as follow:

base class:

class AgentInfo {
public:
    Agent * agent;
    bool valid;
    AgentInfo(Agent * a_, bool valid = true);
    AgentInfo();
};


//helper tags used in multi_index_container
struct agent_tag{};
struct agent_valid{};

a class declaring a multi_index_container:

class AgentsList {
public:

private:

//main definition of the container
typedef boost::multi_index_container<
        AgentInfo, indexed_by<
        random_access<>//0
,hashed_unique<tag<agent_tag>,member<AgentInfo, Agent *, &AgentInfo::agent> >//1
,hashed_non_unique<tag<agent_valid>,member<AgentInfo, bool, &AgentInfo::valid> >//2

        >
        > ContainerType;

//the main storage
ContainerType data;

//easy reading only
typedef typename nth_index<ContainerType, 1>::type Agents;
public:
    //  given an agent's reference, returns a const version of a single element from the container.
    const AgentInfo  &getAgentInfo(const Agent * agent, bool &success)const {
        success = false;
        AgentsList::Agents &agents = data.get<agent_tag>();
        AgentsList::Agents::iterator it;
        if((it = agents.find(agent)) != agents.end())//<-- error in .find()
        {
            success = true;
        }
        return  *it;
    }
};

My problem is in the getAgentInfo() which is a sort of accessor method. the error is obvious:

error: invalid conversion from ‘const Agent*’ to ‘Agent*’
  • I cant input a non constant Agent* coz I am getting it from other part of the code base.

  • I dont like to use const_cast

is there any way to call the .find() method using the constant Agent? thank you

No correct solution

OTHER TIPS

Define your index 1 as

hashed_unique<
  tag<agent_tag>,
  member<AgentInfo, Agent *, &AgentInfo::agent>,
  boost::hash<const Agent*>, std::equal_to<const Agent*>
>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top