2013-04-18 150 views
3

所以我得到一個奇怪的錯誤,當我編譯我的程序:缺少';'前「模板<」

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

+3

您是否包含另一個在類定義之後沒有分號的頭? – chris

+11

我的錢是你不向我​​們展示的代碼:) – jrok

+0

你'#include'ing任何頭? – 0x499602D2

回答

11

該錯誤可能是有點誤導。

這不一定是非常重要的一個;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() 
{ 
} 
+0

這是一個瘋狂的猜測:) –

+4

@WouterHuysentruit,這是最合乎邏輯的一個,真的。 – chris

+3

@WouterHuysentruit也許是一個猜測,但我敢打賭一些切達是正確的。 – WhozCraig