2012-11-25 2 views
0

은 다음과 같다 :priority_queue 상수 식 코드에서

을 비교 한 알고리즘

class PathComp{ 
public: 
virtual bool betterThan(const PathInfo& path1, const PathInfo& path2) const{ 
//returns true if path1 is shorther than path2 
{ 
}; 

재정의 연산자()

class QueueComp{ 
private: 
PathComp* comp; 
public: 
QueueComp(PathComp* pc); 
bool operator() (const pair<PathInfo, string>& item1, const pair<PathInfo, string> item2); 
}; 

QueueComp::QueueComp(PathComp* pc):comp(pc){} 
bool QueueComp::operator() (const pair<PathInfo, string>& item1, const pair<PathInfo, string>& item2){ 
    return comp->betterThan(item1.first, item2.first); 
} 

기능 사용 우선 순위 큐

list<string> Graph::shortestPath(const string& from, const string& to, PathComp* pc) const{ 
    const QueueComp comp(pc); 
    std::priority_queue<pair<PathInfo, string>, set<pair<PathInfo, string> >, comp> q; 
} 

를 가진 클래스 컴파일러 토르가 오류 메시지를 표시합니다. 'comp'가 상수 표현식에 나타날 수 없습니다. 템플릿 인수 3이 유효하지 않습니다. 이전에 선언에 잘못된 유형이 있습니다. 토큰

누구에게 문제가 있는지 알고 있습니까? 모든 도움에 감사드립니다.

답변

1

컴파일러는 이미 문제가 무엇인지 말합니다. 비 constexpr은 템플릿 인수가 될 수 없습니다. 당신은 아마 즉각적인 문제는 comp 개체하지만 템플릿이 비교 함수에 대한 유형을 기대이었다

std::priority_queue<std::pair<PathInfo, std::string>, 
        std::vector<std::pair<PathInfo, std::string> >, 
        QueueComp> 
    q(comp); 

를 작성하는 것을 의미했다. 이 문제가 해결되면 std::set<...>std::priorit_queue<...>과 함께 사용할 수있는 컨테이너가 아니며 std::vector<...>은 사용할 수있는 컨테이너가 아닙니다.

+0

덕분에 내가 필요한 모든 것이 었습니다. 당신은 저를 많이 도와주었습니다. – franz9