2015-11-16 69 views
1

我對Heap.h和cpp文件以及PQ中的前面的代碼做了一些更改,並添加了PQ.cpp。我正在嘗試創建一個堆ADT,它將成爲一個優先級隊列的堆實現。我只是試圖添加一個peek函數堆說明,但我不能沒有收到錯誤。 其中包含這些錯誤。如何添加peek函數到我的堆類?

爲架構x86_64的未定義符號: 「PrecondViolatedExcep :: PrecondViolatedExcep(STD :: __ 1 :: basic_string的,性病:: __ 1 ::分配器>常量&)」,從引用: pqClass :: PEEK()const的在PQ.o Heap.o中的heapClass :: peekTop()const 「itemClass :: GetDirection()const」,引用來自: enqRequest(int,int,int,bool,itemClass &,pqClass,pqClass)in main。 o ld:找不到架構x86_64的符號 clang:錯誤:連接器命令失敗,退出代碼1(使用-v查看調用)

Heap.h

// ********************************************************* 
// Header file Heap.h for the ADT heap. 
// ********************************************************* 

#include "Data.h" // definition of itemClass 

#pragma once 

const int MAX_HEAP = 20; 
typedef itemClass keyType; 

typedef itemClass heapItemType; 

class heapClass 
{ 
public: 
heapClass(); // default constructor 
// copy constructor and destructor are 
// supplied by the compiler 

// heap operations: 
virtual bool HeapIsEmpty() const; 
// Determines whether a heap is empty. 
// Precondition: None. 
// Postcondition: Returns true if the heap is empty; 
// otherwise returns false. 

virtual void HeapInsert(const heapItemType& NewItem, 
         bool& Success); 
// Inserts an item into a heap. 
// Precondition: NewItem is the item to be inserted. 
// Postcondition: If the heap was not full, NewItem is 
// in its proper position and Success is true; 
// otherwise Success is false. 

virtual void HeapDelete(heapItemType& RootItem, 
         bool& Success); 
// Retrieves and deletes the item in the root of a heap. 
// This item has the largest search key in the heap. 
// Precondition: None. 
// Postcondition: If the heap was not empty, RootItem 
// is the retrieved item, the item is deleted from the 
// heap, and Success is true. However, if the heap was 
// empty, removal is impossible and Success is false. 

heapItemType peekTop() const throw(PrecondViolatedExcep); 
protected: 
void RebuildHeap(int Root); 
// Converts the semiheap rooted at index Root 
// into a heap. 

private: 
heapItemType Items[MAX_HEAP]; // array of heap items 
int   Size;    // number of heap items 
}; // end class 
// End of header file. 

Heap.cpp

// ********************************************************* 
// Implementation file Heap.cpp for the ADT heap. 
// ********************************************************* 
#include "Heap.h" // header file for heap 

heapClass::heapClass() : Size(0) 
{ 
} // end default constructor 

bool heapClass::HeapIsEmpty() const 
{ 
return bool(Size == 0); 
} // end HeapIsEmpty 

void heapClass::HeapInsert(const heapItemType& NewItem, 
         bool& Success) 
// Method: Inserts the new item after the last item in the 
// heap and trickles it up to its proper position. The 
// heap is full when it contains MAX_HEAP items. 
{ 
Success = bool(Size < MAX_HEAP); 

if (Success) 
{ // place the new item at the end of the heap 
    Items[Size] = NewItem; 

    // trickle new item up to its proper position 
    int Place = Size; 
    int Parent = (Place - 1)/2; 
    while ((Parent >= 0) && 
      (Items[Place].Key() > Items[Parent].Key())) 
    { // swap Items[Place] and Items[Parent] 
     heapItemType Temp = Items[Parent]; 
     Items[Parent] = Items[Place]; 
     Items[Place] = Temp; 

     Place = Parent; 
     Parent = (Place -1)/2; 
    } // end while 

    ++Size; 
} // end if 
} // end HeapInsert 

void heapClass::HeapDelete(heapItemType& RootItem, 
         bool& Success) 
// Method: Swaps the last item in the heap with the root 
// and trickles it down to its proper position. 
{ 
Success = bool(!HeapIsEmpty()); 

if (Success) 
{ RootItem = Items[0]; 
    Items[0] = Items[--Size]; 
    RebuildHeap(0); 
} // end if 
} // end HeapDelete 

void heapClass::RebuildHeap(int Root) 
{ 
// if the root is not a leaf and the root's search key 
// is less than the larger of the search keys in the 
// root's children 
int Child = 2 * Root + 1; // index of root's left 
// child, if any 
if (Child < Size) 
{ // root is not a leaf, so it has a left child at Child 
    int RightChild = Child + 1; // index of right child, 
    // if any 

    // if root has a right child, find larger child 
    if ((RightChild < Size) && 
     (Items[RightChild].Key() > Items[Child].Key())) 
     Child = RightChild; // index of larger child 

    // if the root's value is smaller than the 
    // value in the larger child, swap values 
    if (Items[Root].Key() < Items[Child].Key()) 
    { heapItemType Temp = Items[Root]; 
     Items[Root] = Items[Child]; 
     Items[Child] = Temp; 

     // transform the new subtree into a heap 
     RebuildHeap(Child); 
    } // end if 
    } // end if 

    // if root is a leaf, do nothing 
} // end RebuildHeap 

heapItemType heapClass::peekTop() const throw(PrecondViolatedExcep) 
{ 
    if (HeapIsEmpty()) 
    throw PrecondViolatedExcep("Attempted peek into an empty heap."); 

    return Items[0]; 
} // end peekTop 

優先級隊列 PQ.h

// ********************************************************* 
    // Header file PQ.h for the ADT priority queue. 
// Heap implementation. 
// ********************************************************* 
#include "Heap.h" // ADT heap operations 

typedef heapItemType pqItemType; 

class pqClass 
    { 
    public: 
    // default constructor, copy constructor, and 
    // destructor are supplied by the compiler 

// priority-queue operations: 
virtual bool PQueueIsEmpty() const; 
virtual void PQueueInsert(const pqItemType& NewItem, 
          bool& Success); 
virtual void PQueueDelete(pqItemType& PriorityItem, 
          bool& Success); 

pqItemType peek() const throw(PrecondViolatedExcep); 

private: 
heapClass H; 
}; // end class 
// End of header file. 

PQ.cpp

#include <stdio.h> 
// ********************************************************* 
// Implementation file PQ.cpp for the ADT priority queue. 
// A heap represents the priority queue. 
// ********************************************************* 
#include "PQ.h" // header file for priority queue 

bool pqClass::PQueueIsEmpty() const 
{ 
    return H.HeapIsEmpty(); 
} // end PQueueIsEmpty 

void pqClass::PQueueInsert(const pqItemType& NewItem, 
         bool& Success) 
{ 
    H.HeapInsert(NewItem, Success); 
} // end PQueueInsert 

void pqClass::PQueueDelete(pqItemType& PriorityItem, 
         bool& Success) 
{ 
    H.HeapDelete(PriorityItem, Success); 
} // end PQueueDelete 

pqItemType pqClass::peek() const throw(PrecondViolatedExcep) { 
    try 
{ 
    return H.peekTop(); 
} 
    catch (PrecondViolatedExcep e) { 
     throw PrecondViolatedExcep("Attempted peek into an empty priority  queue."); } // end try/catch 
} // end peek 
// End of implementation file. 

回答

1

您需要添加一個const到您r .cpp文件中的函數簽名,使其與.h文件中的簽名匹配。

+0

此外,請參閱有關如何改進功能實現的其他答案。 –

0

你在PQ.h創建abstruct類的一個對象:

heapClass H; 

還你想只有當堆是空的訪問元素

heapItemType heapClass::peekTop(){ 
    if (HeapIsEmpty()) 

    return heapItemType[0]; 
} 

,你需要這樣的事(如果你的類型是指針):

heapItemType heapClass::peekTop(){ 
    if (HeapIsEmpty()) { 
    return nullptr; 
    } 

    return heapItemType[0]; 
} 

或者你需要某種無效的對象,你可以在c中返回空堆

+1

他將不得不將函數定義更改爲const以擺脫編譯器錯誤。 –

0

的ASE從我可以看你的代碼,告訴您已經定義了你的heapClass的方法,例如:

virtual heapItemType peekTop() const =0; 

這被聲明爲純虛函數,因爲你是將它設置在類中的聲明中等於0。這會使您的heapClass變得抽象。我在pqClass中看到的所有內容都是您存儲heapClass類型的對象。在這裏我沒有看到任何繼承,這是你可能會遇到構建或編譯器錯誤的原因之一。你不能在一個類中聲明一個方法或函數,而不從它繼承的情況下是純虛擬的。我不知道這是你的意圖還是不是。但是,現在你的heapClass是抽象的這種類型的方法聲明!

您需要從其聲明中刪除= 0或從此類繼承,派生類將使用override指令實現此功能。另外匿名郵件是正確的,你還需要在你的實現文件中聲明const這個方法,Andrew Lavq有一個關於你的函數實際實現的有效觀點。

相關問題