2015-04-29 84 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作为前缀。

相关问题