2017-02-28 5 views
1

Movie 오브젝트를 unordered_set<Movie> 컨테이너에 삽입하려고했지만 일치하는 멤버 함수가 없다는 오류가 발생했습니다. 나는이unordered_set에 삽입 할 일치하는 멤버가 없습니다.

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 구조체 모두를 것이다 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

의 키 저장소입니다 다시 빌드하지 말고 그냥 변수를 수정하면됩니다. 따라서 명시 적으로 금지되어 있습니다. unordered_set

iterator Constant ForwardIterator 
const_iterator Constant forward iterator 
+1

왜 내가 이것을 기억하지 못했는지 알 수 없습니다. 만약 내가 올바르게 기억한다면, set iterators (주문한 것과 그렇지 않은 것 모두)가 C++ 11에서이 매우 문제 (set containment를 수정하는 사람들)를 막기 위해'const'하게되었다. (여기 늦었고 피곤하지만 나는 생각한다. 그것이 잡았던 때이었다). – WhozCraig

+0

감사합니다. –

+0

@ KesongXie가지도를 사용하거나 주문하지 않고 대신 관련 객체에 관심이있는 유일한 객체 (이름)를 사용합니다. 이 경우 값에 매핑 된 값은 자유롭게 수정할 수 있습니다. 고려할만한 가치가있다. – WhozCraig