2013-11-15 24 views
1

編輯當我重新啓動一切從頭開始後固定它。我不確定是什麼導致了這個問題。如果任何人有任何想法或洞察是什麼導致了主要問題,我會編輯第一篇文章和後續。C++模板類錯誤與頭文件中引用的實現類的

所以我在做家庭作業創建使用模板能夠同時使用數值數據和字符串堆類。我使用的編譯器是Visual Studio 2010中

頭文件看起來像這樣..

#ifndef HEAP_H 
#define HEAP_H 

#include <iostream> 
#include <vector>  // not needed if you use a static array 

using std::cout; 
using std::endl; 
using std::vector; 
template <typename type> 
class Heap { 
... the method headers I have to implement in the Heap.template file 
}; 
#include "Heap.template" 

#endif 

的Heap.template文件是我們應該執行堆的方法。但是,我不能在沒有被錯誤屠殺的情況下進行編譯。這裏是由教師本人提供的第一種方法:

template <typename type> 
Heap<type>::Heap(bool maxheap) { 
// this default constructor supports a dynamic array. In this array, the root 
// of the heap begins at index 1; the variable "dummy" is used to fill the 
// zero position of the dynamic array. 
type dummy; 
    this->maxheap = maxheap; 
    heap.push_back(dummy); 
    size = heap.size()-1; 
} 

即使我註釋掉的,我已經實現的方法休息,我仍然帶有錯誤

error C2143: syntax error : missing ';' before '<' 
error C4430: missing type specifier - int assumed. Note: C++ does not support default-  int 
error C2988: unrecognizable template declaration/definition 
error C2059: syntax error : '<' 
上線

Heap<type>::Heap(bool maxheap) { 

這些相同的一組錯誤的爲每一個方法我試圖在供應.template文件來實現存在,例如該印刷方法

template <typename type> 
void Heap<type>::PrintHeap() 
{ 
for(std::vector<type>::iterator i = heap.begin(); i != heap.end(); ++i) 
{ 
    cout << *i << ' '; 
} 
} 

給我與他提供的方法相同的一組錯誤。我現在非常困惑,真的不知道是什麼原因造成了這個問題。我會很感激一些見解,謝謝!

回答

0

當您所定義的成員函數,否則不應包括在你的類<type>。這樣做:

template <typename type> 
Heap::Heap(bool maxheap) { 
    //... 
} 

編譯器已經知道了類使用,因爲template聲明的那些模板參數。

+0

嗯,我試過這個,它並沒有解決問題。我從頭開始重新開始,創建一個新的空項目,重新下載文件,錯誤不再存在。所以我真的不知道我做了什麼導致它。 也許值得注意的是,在我的Heap.template中,如果我在我的成員函數中省略了,它實際上給了我一個錯誤,並且只在我按照堆 :: Function()的格式進行編譯。 – LinusRed