2010-11-04 92 views
2

我想跟着我的大學筆記,我試着用google搜索錯誤,看着stackover流,但我似乎無法弄清楚什麼是錯的。C++模板 - 基礎知識

我已經閱讀了很多地方,你需要在一個文件(頭文件)中包含實現和規範文件,所以我這樣做了。我只是從我印刷的幻燈片中複製和粘貼,並使用Google搜索並嘗試複製頁面上寫的內容,但仍然出現錯誤。我正在使用g ++編譯器。

無論如何,這裏是我的代碼。

template<class A_Type> 
class calc 
{ 
    public: 
    A_Type multiply(A_Type x, A_Type y); 
    A_Type add(A_Type x, A_Type y); 
}; 

template<class A_type> 
A_Type calc<A_Type>::multiply(A_Type x, A_Type y) 
{ 
    return x*y; 
} 
template<class A_Type> 
A_Type calc<A_Type>::add(A_Type x, A_Type y) 
{ 
    return x+y; 
} 

而我得到的錯誤:預期的構造函數,析構函數或類型「鈣」之前轉換(上test.h的第10行)

我缺少的東西?我不明白

回答

5
template<class A_type>       // lowercase t in A_type 
A_Type calc<A_Type>::multiply(A_Type x, A_Type y) // uppercase T's in A_Type 
+0

謝謝,一直在看這個至少一個小時。欣賞它 – 2010-11-04 02:08:17

0

你的多重定義說template <class A_type>(小寫字母「T」),那麼你用大寫的其他地方。