2017-02-28 6 views
1

Movieオブジェクトをunordered_set<Movie>コンテナに挿入しようとしましたが、このような一致するメンバー関数が存在しないというエラーが表示されました。私はこのunordered_setのinsertに一致するメンバーがありません

void ActorGraph::addActor(string actor_name, string movie_title, int movie_year){ 
    unordered_map<string, unordered_set<ActorNode>>::iterator con_itr = connections.find(actor_name); 
    ActorNode actor(actor_name); 
    Movie movie(movie_title, movie_year); 

    if(con_itr != connections.end()){ 
     auto adjSet = con_itr->second; 
     unordered_set<ActorNode>::iterator act_itr = adjSet.find(actor); 
     if(act_itr != adjSet.end()){ 
      //in the set 
      auto mov_itr = act_itr->movies.find(movie); 
      if(mov_itr == act_itr->movies.end()){ 
       act_itr->movies.insert(movie) //no matching function, while act_itr->movies is of type unordered_set<Movie> 
      } 
     } 
    }else{ 
     unordered_set<ActorNode> adjSet; 
     actor.movies.insert(movie); 
     adjSet.insert(actor); 
     connections[actor_name] = adjSet; 
     cout << "The size is: " << actor.movies.size() << endl; 
    } 
} 

マイActorNodeのようなものが

#ifndef Movie_h 
#define Movie_h 

#include <iostream> 
using namespace std; 

struct Movie{ 
    string name; 
    int year; 
    Movie(string n, int y): name(n), year(y){} 
    bool operator ==(const Movie &m) const; 
}; 



namespace std 
{ 
    template <> 
    struct hash<Movie> 
    { 
     size_t operator()(const Movie& movie) const 
     { 
      return hash<std::string>{}(movie.name + to_string(movie.year)); 
     } 
    }; 
} 


#endif /* Movie_ph*/ 

enter image description here

は、私が実装したこの

struct ActorNode{ 
    //the name of the actor/actress 
    string name; 

    /** the movie that this actor/actree participated in 
    */ 
    unordered_set<Movie> movies; 

    ActorNode(string n) : name(n){} 

    bool operator ==(const ActorNode &other) const; 
}; 


namespace std 
{ 
    template <> 
    struct hash<ActorNode> 
    { 
     size_t operator()(const ActorNode& actor) const 
     { 
      return hash<std::string>{}(actor.name); 
     } 
    }; 
} 

作品の構造体のように見える構造体で行なったし、演算子をオーバーライドし、私のMovieとActorNodeの両方の構造体を、ここでunordered_set

のキーはリポジトリです: Repo


Minimal Reproduction Example

#include <iostream> 
#include <string> 
#include <unordered_set> 

struct Movie{ 
    std::string name; 
    int year; 

    Movie(std::string n, int y): name(std::move(n)), year(y) 
    { 
    } 

    bool operator ==(const Movie &m) const 
    { 
     return year == m.year && name == m.name; 
    }; 
}; 


namespace std 
{ 
    template <> 
    struct hash<Movie> 
    { 
     size_t operator()(const Movie& movie) const 
     { 
      return hash<std::string>{}(movie.name + to_string(movie.year)); 
     } 
    }; 
} 
//////////////////// 

struct ActorNode 
{ 
    std::string name; 
    std::unordered_set<Movie> movies; 

    ActorNode(std::string n) : name(std::move(n)) 
    { 
    } 

    bool operator ==(const ActorNode &other) const 
    { 
     return name == other.name; 
    } 
}; 


namespace std 
{ 
    template <> 
    struct hash<ActorNode> 
    { 
     size_t operator()(const ActorNode& actor) const 
     { 
      return hash<std::string>{}(actor.name); 
     } 
    }; 
} 
//////////////////// 


int main() 
{ 
    std::unordered_set<ActorNode> actors; 
    actors.emplace("Gene Wilder"); 

    auto itr = actors.find(ActorNode("Gene Wilder")); 
    if (itr != actors.end()) 
    { 
     // error: no matching function for call to     
     // 'std::unordered_set<Movie>::insert(Movie) const' 
     itr->movies.insert(Movie("Stir Crazy", 1980)); 
    } 
} 
+0

におけるイテレータの定義は、最小限の完全な例を教えてください。そして、エラーメッセージ全体を教えてください。 – JHBonarius

+0

Btw:エラーのある行の末尾に ';'がありません。 – JHBonarius

+0

@WhozCraigが追加されました –

答えて

2

問題は、あなたがそれを再構築setにつながることができます原因、setのキーを変更しますが、set意志することができないということです再構築しないでください。ただ変数だけを変更するだけです。したがって、明示的に禁止されています。

unordered_set

iterator Constant ForwardIterator 
const_iterator Constant forward iterator 
+1

なぜ私はこれを覚えていないのか分かりません。私が正しいことを覚えていれば、set iterator(順序付けされたものでもそうでなくても)は、C++ 11でこの非常に問題(set containmentを変更している人)を防ぐために 'const'にされました(ここに遅れています。それが保留になったときだった)。 – WhozCraig

+0

これについてよく知っていただきありがとうございます。 –

+0

@KesongXieはマップ(順序付けされているかどうか)を使用して、関心のあるもの(名前)を関連オブジェクトにマッピングします。その場合、値へのマップは自由に変更可能です。検討する価値がある。 – WhozCraig

関連する問題