2010-11-04 91 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”),那么你用大写的其他地方。