-1

次のように、クラスのメンバーにインデックスを付ける非常に単純なマルチ インデックス コンテナーがあります。

基本クラス:

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

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

私の問題はgetAgentInfo()、一種のアクセサメソッドです。エラーは明らかです:

error: invalid conversion from ‘const Agent*’ to ‘Agent*’
  • Agent*コードベースの他の部分から取得しているため、非定数を入力できません。

  • 使いたくないconst_cast

.find()定数エージェントを使用してメソッドを呼び出す方法はありますか? ありがとうございました

4

1 に答える 1