liuxin95 2018-09-21 02:56 采纳率: 0%
浏览 894
已采纳

类模板 重载输出符 unresolved external symbol

写了一个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;
}
  • 写回答

1条回答 默认 最新

  • dabocaiqq 2018-09-22 08:53
    关注
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 在若依框架下实现人脸识别
  • ¥15 网络科学导论,网络控制
  • ¥100 安卓tv程序连接SQLSERVER2008问题
  • ¥15 利用Sentinel-2和Landsat8做一个水库的长时序NDVI的对比,为什么Snetinel-2计算的结果最小值特别小,而Lansat8就很平均
  • ¥15 metadata提取的PDF元数据,如何转换为一个Excel
  • ¥15 关于arduino编程toCharArray()函数的使用
  • ¥100 vc++混合CEF采用CLR方式编译报错
  • ¥15 coze 的插件输入飞书多维表格 app_token 后一直显示错误,如何解决?
  • ¥15 vite+vue3+plyr播放本地public文件夹下视频无法加载
  • ¥15 c#逐行读取txt文本,但是每一行里面数据之间空格数量不同