![](https://img-blog.csdnimg.cn/20201014180756780.png?x-oss-process=image/resize,m_fixed,h_64,w_64)
模板
weixin_46060711
c/c++
展开
-
在类模板中,利用友元函数重载输出运算符 << 时,不加命名空间std会报错,不加 <T> 也会报错
类的头文件:Vector0.h #pragma once #include <iostream> //using namespace std; template <typename T> class Vector0 { public: Vector0(int size = 0); Vector0(const Vector0<T> &other); ~Vector0(void); Vector0& operator=(const Vect原创 2022-05-04 02:39:36 · 431 阅读 · 0 评论 -
友元函数类模板的写法
// demo 15-11.c #include <iostream> using namespace std; template <typename T> class A { public: A(T t=0); //声明一个友元函数,实现对两个A类对象进行加法操作 template <typename T> friend A<T> addA(const A<T> &a, const A<T> &b);原创 2022-05-01 18:26:00 · 481 阅读 · 0 评论 -
类模板的声明(.h文件)和实现(.cpp 或.hpp文件)完全分离时,在主函数的实现文件(.cpp)中应该包含的是类模板的实现(.cpp 或.hpp文件)
注意:当类模板的声明(.h文件)和实现(.cpp 或.hpp文件)完全分离,因为类模板的特殊实现,我们应在使用类模板时使用#include 包含 实现部分的.cpp 或.hpp文件。 demo.h: #pragma once template <typename T> class A { public: A(T t=0); T &getT(); A operator +(const A &other); void print(); private: T t;原创 2022-05-01 18:21:52 · 696 阅读 · 0 评论