类模板说明与类模板分开,本代码在Linux 环境下编译通过
1.主函数:
/*************************************************
* copyright @ zjl 2018-2020.All rights reserved
* Filename :数组类,模板简单实现
* Atuhor: zhong
* Version:v1.0
* Description:设计一个数组类模板(myvector) ,完成对int ,char Teacher的数据元素管理
* others:需求完成类模板,构造函数,重载<< >> =
* Log:
*
* ***********************************************/
#include <iostream>
#include <assert.h>
#include "string.h"
#include </home/zjl/ProTemp/f1/f1.cpp>
#include <string>
using namespace std;
template<typename T>
class MyVector;
template <class T>
ostream& operator << (ostream &,const MyVector<T> &);
class Teacher{
public:
Teacher()
{
this->age=33;
strcpy(name," ");
}
Teacher(char *name,int age){
this->age=age;
strcpy(this->name,name);
}
void printT(){
cout<<"name:"<<name<<"- age:"<<age<<endl;
}
private:
int age;
char name[32];
};
//test 数组拷贝
int main()
{
//test1
MyVector<int> myv1(10);
for (int i = 0; i < myv1.getLen(); i++)
{
myv1[i] = i + 1;
}
// cout << myv1[i] << " ";
// cout << endl;
cout << "-----myv1-----" << endl;
cout << myv1;
MyVector<int> myv2 = myv1; //重载=
for (int i = 0; i < myv1.getLen(); i++)
{
cout << myv2[i] << " ";
}
cout << "-----myv2-----" << endl;
cout << myv2 << endl;
cout << "Hello World" << endl;
//test2
cout<<"-----test2-------"<<endl;
MyVector<char> myv3(10);
myv3[0]='a';
myv3[1]='b';
cout<<myv3<<endl;
//test3
cout<<"------test3-------"<<endl;
Teacher t1("t1",33);
Teacher t2("t2",32);
MyVector<Teacher> tArray(4);
tArray[0]=t1; //采用的是默认的浅拷贝构造函数
tArray[1]=t2;
for(int i=0;i<4;i++){
Teacher tmp=tArray[i];
tmp.printT();
}
return 1;
}
2.myinclude.h
#pragma once
#include <iostream>
using namespace std;
template <typename T>
class MyVector;
template <typename T>
ostream& operator<< (ostream &,const MyVector<T>&);
template <typename T>
class MyVector
{
// 需要注意 一定要加上 <T> 但是在.cpp文件里面加上了 <T> 还会报错 MyVector<T> 可以不加 <T> 编译也能通
friend ostream & operator<< <T>(ostream &out, const MyVector<T> &obj);
public:
MyVector(int size = 0); // 构造函数
MyVector(const MyVector &obj); // 拷贝构造函数
~MyVector(); // 析构函数
public:
T & operator[] (int index);
MyVector & operator= (const MyVector &obj);
public:
int getLen()
{
return m_len;
}
public:
T *m_space;
int m_len;
};
3.f1.cpp
#include <iostream>
using namespace std;
#include "../include/myinclude.h"
template <typename T>
MyVector<T>::MyVector(int size) // 不允许使用默认参数
{
// m_space = new int[size];
m_space = new T[size];
m_len = size;
}
// 拷贝构造函数
template <typename T>
MyVector<T>::MyVector(const MyVector &obj)
{
// 根据obj的大小分配内存
m_len = obj.m_len;
m_space = new T[m_len];
// copy 数据
for (int i = 0; i < m_len; i++)
{
m_space[i] = obj.m_space[i];
}
}
template <typename T>
MyVector<T>::~MyVector()
{
if (m_space != NULL)
{
delete[] m_space;
m_space = NULL;
m_len = 0;
}
}
template <typename T>
T & MyVector<T>::operator[] (int index)
{
return m_space[index];
}
template <typename T>
MyVector<T> & MyVector<T>::operator= (const MyVector<T> &obj)
{
// 先把自己的内存给释放掉
if (m_space != NULL)
{
delete[] m_space;
m_space = NULL;
m_len = 0;
}
// 根据 obj 分配内存
m_len = obj.m_len;
m_space = new T[m_len];
// copy 数据
for (int i = 0; i < m_len; i++)
{
m_space[i] = obj[i];
}
return *this;
}
// 重载 << 操作符
template <typename T>
ostream & operator<< (ostream &out, const MyVector<T> &obj)
{
for (int i = 0; i < obj.m_len; i++)
{
out << obj.m_space[i] << " ";
}
out << endl;
return out;
}
之前报错:
原因是:没有在主函数实例化之间进行友元函数声明,注意模板函数做友元要显式声明