C++模板 - 基础知识

Ste*_*a D 2 c++ templates

我正在尝试按照我的大学笔记,我尝试使用谷歌搜索错误并查看堆栈流程,但我似乎无法弄清楚什么是错误的.

我已经阅读了很多地方,你需要在一个文件(标题)中同时包含实现和规范文件,所以我已经这样做了.我只是从我的打印幻灯片中复制并粘贴,然后用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;
}
Run Code Online (Sandbox Code Playgroud)

我得到错误:在'calc'之前预期的构造函数,析构函数或类型转换(在test.h的第10行)

我错过了什么吗?我不懂

Jam*_*lis 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
Run Code Online (Sandbox Code Playgroud)