写了一个ArryList的类,在.h中进行声明,.cpp文件中实现,在.h文件中对输出符重载进行了友元声明,在.cpp的最后面对输出符重载进行了实现。但是报错:
main.obj : error LNK2019: unresolved external symbol "class std::basic_ostream > & __cdecl operator<<(class std::basic_ostream > &,class arryList const &)" (??6@YAAAV?$basic_ostream@DU?$char_traits@D@std@@@std@@AAV01@ABV?$arryList@H@@@Z) referenced in function _main
ArryList.h
#ifndef ARRYLIST_H
#define ARRYLIST_H
#include<iostream>
template<class T>
class arryList
{
friend std::ostream& operator<<(std::ostream& out, const arryList<T>& a);
public:
arryList(int size = 10);
arryList(const arryList<T>& L1);
~arryList();
bool isFull();
void insert(int index, T ele);
int len();
int size();
T& get(int index) const;
private:
T* element;
int lenth; //数组容量
int listsize; //数组存储元素个数
};
#endif // !ARRYLIST_H
ArryList.cpp
#include"ArryList.h"
#include <algorithm>
using namespace std;
template<class T>
arryList<T>::arryList(int size) :lenth(size)
{
element = new T[lenth];
listsize = 0;
}
template<class T>
arryList<T>::arryList(const arryList<T>& L1)
{
this->lenth = L1.lenth;
this->listsize = L1.listsize;
this->element = new T[lenth];
copy(L1.element, L1.element + listsize, this->element);
}
template<class T>
arryList<T>::~arryList()
{
delete[] element;
}
template<class T>
bool arryList<T>::isFull()
{
if (lenth == listsize)
{
return 1;
}
else
{
return 0;
}
}
template<class T>
void arryList<T>::insert(int index, T ele)
{
if (index < 0 || index>=lenth)
{
throw "插上入位置越界";
}
if (isFull())
{
T* temp = new T[lenth*2];
// copy(element, element + lenth, temp);
for (size_t i = 0; i < lenth; i++)
{
temp[i] = element[i];
}
lenth = lenth * 2;
delete[] element;
element = temp;
}
if (index<=listsize)
{
// copy(element + index, element + listsize, element + index + 1);
for (size_t i = listsize; i > index; i--)
{
element[i] = element[i - 1];
}
}
element[index] = ele;
listsize++;
}
template<class T>
int arryList<T>::len()
{
return lenth;
}
template<class T>
int arryList<T>::size()
{
return listsize;
}
template<class T>
T& arryList<T>::get(int index) const
{
return element[index];
}
//重载输出符;
template<class T>
ostream& operator<<(ostream& out, const arryList<T>& a)
{
for (size_t i = 0; i < a.listsize; i++)
{
out << a.element[i]<< " ";
}
return out;
}