2013-08-06 2 views
1

Dijkstra 알고리즘을 구현하려고합니다. 나는이 priority_queueDijkstras algorithm with priority_queue

priority_queue<pair<PathInfo,string>,vector<pair<PathInfo,string> >,QueueComp> p; 

을 사용하고 어디에

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

내 "비교"기능입니다. 오류는 QueueComp 에는 기본 생성자이없고 을 만들 수 없습니다. 내 코드를 컴파일하려면 어떻게해야합니까? BTW이이이이이 pathcomppl.cpp

#include "pathcomppl.h" 

bool PathCompPL::betterThan(const PathInfo& path1,const PathInfo& path2){ 
    if (path1.getTotalPrice()>path2.getTotalPrice()) 
     return true; 

    if (path1.getTotalPrice()==path2.getTotalPrice() && path1.getTotalLength()>path2.getTotalLength()) 
     return true; 

    return false; 
} 
에게 있습니다

#include "pathcomp.h" 

class PathCompPL:public PathComp{ 
public: 
virtual bool betterThan(const PathInfo& path1,const PathInfo& path2); 
}; 

pathcomppl.h에게 있습니다

class PathComp{ 
public: 
    virtual bool betterThan(const PathInfo& path1,const PathInfo& path2)=0; 
}; 

pathcomp.h에게 있습니다

error: no matching function for call to 'QueueComp::QueueComp()' 

오류입니다

확장 된 오류 메시지

main.cpp: In constructor ‘std::priority_queue<_Tp, _Sequence, _Compare>::priority_queue(const _Compare&, const _Sequence&) [with _Tp = std::pair<PathInfo, std::basic_string<char> >; _Sequence = std::vector<std::pair<PathInfo, std::basic_string<char> > >; _Compare = QueueComp]’: 
main.cpp:11:87: error: no matching function for call to ‘QueueComp::QueueComp()’ 
main.cpp:11:87: note: candidates are: 
In file included from main.cpp:5:0: 
queuecomp.h:14:5: note: QueueComp::QueueComp(PathComp*) 
queuecomp.h:14:5: note: candidate expects 1 argument, 0 provided 
queuecomp.h:10:7: note: QueueComp::QueueComp(const QueueComp&) 
queuecomp.h:10:7: note: candidate expects 1 argument, 0 provided 
+0

QueueComp 유형에 기본 생성자가 필요하다고 생각합니다. – Borgleader

+0

그것은 그렇게 보인다. 그러나 나는 하나를 사용할 것을 권한다. – Slazer

+0

우선 순위 큐에 에지 가중치를 저장하여 항상 가장 짧은 에지를 선택할 수 있습니다. – vik

답변

2

기본값이 아닌 생성자가 있으므로 추가 매개 변수를 사용하여 우선 순위 대기열을 초기화해야합니다.

priority_queue<pair<PathInfo,string>,vector<pair<PathInfo,string> >,QueueComp> p(QueueComp(ptrToPathCompObject));

추가 매개 변수 (QueueComp(ptrToPathCompObject))는 당신의 문제를 해결해야한다.

이미 QueueComp 클래스에 operator()을 구현했다고 가정합니다.

+0

고맙습니다. 문제가 해결되었습니다. – Slazer

+0

그것이 당신을 도왔다 고 다행. 건배! – sgun

0

pc라는 변수를 초기화해야하기 때문에 기본 생성자가 없습니다. 이 생성자가 있습니다 :

QueueComp(PathComp*); 

pc가 매개 변수와 연결되도록 구현해야합니다.

첫 번째 요소는 다음 우선 순위이고 두 번째 요소는 우선 순위가 낮고 세 번째 요소는 대기열 비교입니다. 나는 이것이 당신을 돕기를 바랍니다.

+0

나는 PC에 _pc를 할당하기 위해 구현 된 QueueComp (PathComp * _pc)를 가지고 있습니다. 그러나 정확히 어떻게 priority_queue의 세 번째 템플릿 매개 변수가 컴파일되도록해야합니까? – Slazer

+0

new 연산자를 사용하여 내 대답에 언급 된 생성자를 호출하면 QueueComp 객체가 생깁니다. 이 객체는 priority_queue의 세 번째 매개 변수가됩니다. 그래서, 조금 연습하십시오. 먼저 생성자를 호출 해보십시오. 성공한 경우 생성자 호출의 결과를 QueueComp 변수에 할당합니다. 성공하면 priority_queue에서 해당 변수를 사용할 수 있습니다. 당신이 이미 그 일을하는 법을 이미 알고 있다면 모든 것이 간단합니다. –

0

문제가 적절한 비교기를 구현하는 것 같습니다. 다음과 같은 비교기를 만드는 것이 하나의 대안입니다.

struct CompareEdgeWeights : public binary_function<PathInfo*, PathInfo*, bool> 
    { 
     bool operator()(const PathInfo* left, const PathInfo* right) const 
     { 
      return left->getEdgeWeight() > right->getEdgeWeight(); 
     } 
    }; // end struct 

// Priority queue of node edges 
priority_queue<PathInfo*,vector<PathInfo*>,CompareEdgeWeights > * edgePriorityQueue; 

이 구조체를 binary_function에서 상속 받고 operator()를 오버로드 시키십시오. 그런 다음 가장 낮은 가중치 값에서 가장 높은 가중치 값으로 가장자리를 정렬하는 비교 자로 사용할 수 있습니다. 참고 : 구현을 준수하기 위해 약간 조정해야 할 수도 있습니다. 구현을 더 많이 보지 않고도 100 % 올바른 제안을하는 것은 어렵습니다.

+0

내 질문에 구현을 추가했습니다. – Slazer