링크 된 목록을 사용하여 우선 순위 큐를 구현하려고하지만 try/catch와 관련된 문제가 있습니다. 여기템플릿 클래스에 정의 된 예외가있는 try-catch
#ifndef PRIORITYQUEUELINKED_H
#define PRIORITYQUEUELINKED_H
#include "RuntimeException.h"
#include <list>
using namespace std;
template <typename E, typename C> // uses data type and some total order relation
class PriorityQueueLinked {
// code for PriorityQueueLinked
class EmptyPriorityQueueException : public RuntimeException {
public:
EmptyPriorityQueueException() :
RuntimeException("Empty priority queue") {}
};
// more code
#endif
RuntimeException의 헤더 파일입니다 : 여기
#ifndef RUNTIMEEXCEPTION_H_
#define RUNTIMEEXCEPTION_H_
#include <string>
class RuntimeException {// generic run-time exception
private:
std::string errorMsg;
public:
RuntimeException(const std::string& err) { errorMsg = err; }
std::string getMessage() const { return errorMsg; }
};
inline std::ostream& operator<<(std::ostream& out, const RuntimeException& e)
{
out << e.getMessage();
return out;
}
#endif
가 내 메인 :
이
#include "PriorityQueueLinked.h"
#include "Comparator.h"
#include <iostream>
using namespace std;
int main() {
try {
PriorityQueueLinked<int,isLess> prique; // empty priority queue
prique.removeMin(); // throw EmptyPriorityQueueException
}
catch(...) {
cout << "error" << endl << endl;
}
getchar();
return 0;
}
내 문제가 없습니다에있다 여기에 우선 순위 큐 헤더 파일의 관련 부분은 catch에 대한 "..."대체를 구성 할 수 있습니다. 나는 여러 가지 중 하나를 시도했습니다 : "catch (PriorityQueueLinked < int, isLess> :: EmptyPriorityQueueException E)"이 경우 EmptyPriorityQueueException은 PriorityQueueLinked의 멤버가 아닙니다. 모든 조언을 크게 주시면 감사하겠습니다. 감사
예외는'std :: exception'에서 파생되어야합니다. 또한, 왜 당신은 그것을 내부 클래스로 만들고 있습니까, 그냥 외부에 정의하십시오. –
EmptyPriorityQueueException을 공개로 설정하십시오. 현재이 클래스는 외부에서 볼 수없는 개인 중첩 클래스입니다. – kol
** 헤더 파일에'using namespace std;'**를 절대로 사용하지 마십시오. –