2015-04-29 106 views
-2

我在我的C++代碼中遇到了麻煩,我必須創建一個二進制堆。只要我的「MyHeap.h」文件中有「main」函數,但我的教授希望它在單獨的測試文件中運行,它就可以正常工作。出於某種原因,只要我嘗試將主函數放在「MyHeap.h」文件之外,代碼就不會運行。當它運行時,我得到以下錯誤:錯誤C2143:語法錯誤:缺少';'之前'<'C++

error C2143: syntax error: missing';' before '<' 

我看了看我的代碼,而這正是它說有錯誤,但我看不到任何東西。

// MyHeap.h 
#ifndef _MYHEAP_H 
#define _MYHEAP_H 

#include <vector> 
#include <iterator> 
#include <iostream> 

class Heap { 
public: 
    Heap(); 
    ~Heap(); 
    void insert(int element); 
    int deletemax(); 
    void print(); 
    int size() { return heap.size(); } 
private: 
    int left(int parent); 
    int right(int parent); 
    int parent(int child); 
    void heapifyup(int index); 
    void heapifydown(int index); 
private: 
    vector<int> heap; 
}; 

#endif // _MYHEAP_H 

所以就像我說的,每當我有私有類右後的int main功能,它會工作得很好。現在,當我實現它變成我的測試文件,該文件是這樣的:

#include "MyHeap.h" 
#include <vector> 
#include <iostream> 


int main() 
{ 
    // Create the heap 
    Heap* myheap = new Heap(); 
    myheap->insert(25); 
    myheap->print(); 
    myheap->insert(75); 
    myheap->print(); 
    myheap->insert(100); 
    myheap->print(); 
    myheap->deletemax(); 
    myheap->print(); 
    myheap->insert(500); 
    myheap->print(); 
    return 0; 
} 

它不斷彈出錯誤,我上的任何想法可以去修復這個問題,使我的代碼可以從測試文件運行?

+3

您至少應該突出顯示(例如使用註釋),在您的代碼示例中出現此錯誤。 –

回答

8

使用std::vector而不是vector。編譯器抱怨它不知道vector

由於它位於std命名空間中,最安全的解決方案是以std作爲前綴。

相關問題