我在编译我的程序时遇到了一个奇怪的错误:
Error 1 error C2143: syntax error : missing ';' before ''template<''
我做的每件事都非常标准;没有什么不寻常的:
#ifndef HEAP_H
#define HEAP_H
//**************************************************************************
template<typename TYPE>
class Heap
{
private:
TYPE* heapData;
int currSize;
int capacity;
void _siftUp(int);
void _siftDown(int);
int _leftChildOf(int) const;
int _parentOf(int) const;
public:
Heap(int c = 100);
~Heap();
bool viewMax(TYPE&) const;
int getCapacity() const;
int getCurrSize() const;
bool insert(const TYPE&);
bool remove(TYPE&);
};
不太确定哪里出了问题。我尝试关闭并重新打开我的程序 - 没有运气。使用 Visual Studio 2010
最佳答案
这个错误可能有点误导。
;
不一定重要发生在之前 template<
.
;
在template<
之前发生的实际上是预期的之后 .
这个例子展示了这是如何发生的。
文件 header.h
class MyClass
{
}
文件 heap.h
#ifndef HEAP_H
#define HEAP_H
//**************************************************************************
template<typename TYPE>
class Heap
{
};
#endif
文件 main.cpp
#include "header.h"
#include "heap.h"
int main()
{
}
编辑:
此编译器错误导致您找到错误文件的原因是在 编译之前,预处理器将处理main.cpp
。进入这个单一的字符流。
class MyClass
{
}
//**************************************************************************
template<typename TYPE>
class Heap
{
};
int main()
{
}
关于c++ - 在 ';' 之前缺少 'template<',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16092520/