目前,我正在完成有关存储患者的基于堆的优先级队列的家庭作业。我有一个头文件声明了Patient和Heap类(我必须在一个文件中包括它们)。我有一个非常基本的主要功能,只是测试Heap和Patient类。
#include "maxHeap1.h"
#include <iostream>
using namespace std;
int main() {
Heap h;
Patient p(333,3,4,2);
return 0;
}
但是当我编译并运行时,编译器给出了一个错误消息,提示'maxHeap1.cpp:52:12:error:没有匹配的函数来调用'Patient :: Patient()'堆::堆(){
这是我的头文件和cpp文件:
#ifndef __MAXHEAP1_H
#define __MAXHEAP1_H
const int MAX_HEAP = 2000;
class Patient {
private:
int id;
int priority;
int arrival;
int serviceTime;
public:
Patient(int pId, int pPriority, int pArrival, int pServiceTime) ;
int getId() ;
void setId(int pId) ;
/**
int getPriority() {
return priority;
}
*/
void setPriority(int pPriority) ;
int getArrival() ;
void setArrival(int pArrival);
int getServiceTime();
void setServiceTime(int pServiceTime);
int getKey();
};
class Heap {
public:
Heap(); // default constructor
// copy constructor and destructor are supplied by the compiler
bool heapIsEmpty() const;
void heapInsert(const Patient& p);
void heapDelete(Patient& p);
protected:
void heapRebuild(int root); // Converts the semiheap rooted at
// index root into a heap
private:
Patient patients[MAX_HEAP]; // array of heap items
int size; // number of heap items
};
#endif
我的cpp文件是:
#include <iostream>
#include "maxHeap1.h"
using namespace std;
// Default constructor
Patient::Patient(int pId, int pPriority, int pArrival, int pServiceTime) {
id = pId;
priority = pPriority;
arrival = pArrival;
serviceTime = pServiceTime;
}
int Patient::getId() {
return id;
}
void Patient::setId(int pId) {
id = pId;
}
/**
int getPriority() {
return priority;
}
*/
void Patient::setPriority(int pPriority) {
priority = pPriority;
}
int Patient::getArrival() {
return arrival;
}
void Patient::setArrival(int pArrival) {
arrival = pArrival;
}
int Patient::getServiceTime() {
return serviceTime;
}
void Patient::setServiceTime(int pServiceTime) {
serviceTime = pServiceTime;
}
int Patient::getKey() {
return priority;
}
Heap::Heap() {
size = 0;
}
bool Heap::heapIsEmpty() const {
return (size == 0);
}
void Heap::heapInsert(const Patient& p) {
if (size >= MAX_HEAP) {
cout << "full" << endl;
return;
}
// Place the new item at the end of the heap
patients[size] = p;
// Trickle new item up to its proper position
int place = size;
int parent = (place - 1)/2;
while ( (place > 0) && (patients[place].getKey() > patients[parent].getKey()) ) {
Patient temp = patients[parent];
patients[parent] = patients[place];
patients[place] = temp;
place = parent;
parent = (place - 1)/2;
}
++size;
}
void Heap::heapDelete(Patient& p) {
if (heapIsEmpty()) {
cout << "empty" << endl;
return;
}
else {
p = patients[0];
patients[0] = patients[--size];
heapRebuild(0);
}
}
void Heap::heapRebuild(int root) {
int child = 2 * root + 1; // index of root's left child, if any
if ( child < size ) {
// root is not a leaf so that it has a left child
int rightChild = child + 1; // index of a right child, if any
// If root has right child, find larger child
if ( (rightChild < size) &&
(patients[rightChild].getKey() >patients[child].getKey()) )
child = rightChild; // index of larger child
// If root’s item is smaller than larger child, swap values
if ( patients[root].getKey() < patients[child].getKey() ) {
Patient temp = patients[root];
patients[root] = patients[child];
patients[child] = temp;
// transform the new subtree into a heap
heapRebuild(child);
}
}
}
此错误使我发疯,因此,我们将不胜感激。提前致谢。
添加
class Patient {
int id;
int priority;
int arrival;
int serviceTime;
public:
Patient() = default; // <<<< add this
Patient(int pId, int pPriority, int pArrival, int pServiceTime) ;
int getId() ;
void setId(int pId) ;
/// ...
}