我在我的C ++代码中遇到了麻烦,我必须在其中编写二进制堆。只要我在“ MyHeap.h”文件中具有“ main”功能,它就可以正常工作,但是我的教授希望它在单独的测试文件中运行。出于某种原因,每当我尝试将main函数放置在“ MyHeap.h”文件之外时,该代码都不希望运行。当它运行时,出现以下错误:
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;
}
它不断弹出错误,我有什么想法可以解决此问题,以便我的代码可以从测试文件运行?
使用std::vector
代替vector
。
编译器抱怨它不知道vector
。
由于它位于std
命名空间中,因此最安全的解决方案是给加上前缀std
。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句