简体   繁体   English

C ++模板-基础

[英]C++ templates - basics

I'm trying to follow my college notes, and I've tried googling the error and looking on stackover flow but I can't seem to figure out whats wrong. 我试图遵循我的大学笔记,并且尝试搜索错误并查看stackover流,但似乎无法找出问题所在。

I've read on so many places that you need to have both implementation and specification files in one file (header) so I've done so. 我已经读了很多地方,您需要在一个文件(标头)中同时包含实现文件和规范文件,所以我这样做了。 I've just copy and pasted from my printed slides, and have googled this and tried copying exactly what was written on the page and still I get errors. 我只是从打印的幻灯片上进行复制和粘贴,并在Google上进行了搜索,并尝试完全复制页面上写的内容,但仍然出现错误。 I'm using g++ compiler. 我正在使用g ++编译器。

Anyway, here is my code. 无论如何,这是我的代码。

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;
}

And I get the error: expected constructor, destructor, or type conversion before 'calc' (on line 10 of test.h) 我得到一个错误:'calc'之前的预期构造函数,析构函数或类型转换(在test.h的第10行)

Am I missing something? 我想念什么吗? I dont get it 我不明白

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

您的多重定义说template <class A_type> (小写的“ t”),然后在其他地方使用大写。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM