咻咻咻休 2022-03-28 22:00 采纳率: 100%
浏览 146
已结题

模板的参数变为另一个类后,cout<<没有匹配了


#include  <iostream>
#include  <iomanip>
using  namespace  std;
template  <typename  ElemType>
class  myArrayList
{
private:
    int  mSize;
    int  mLen;
    ElemType* mpArr;
public:
    myArrayList(int  n);
    myArrayList(ElemType* a, int  n);
    myArrayList(myArrayList<ElemType>& other);
    void  show();
    ElemType  getMax();
    void  sort();
    //ostream& operator<<(ElemType& a);
    //*************************
};
template<typename ElemType>
myArrayList<ElemType>::myArrayList(ElemType* a, int n) :mpArr(a), mSize(n) {
    mLen = mSize;
};
template<typename  ElemType>
myArrayList<ElemType>::myArrayList(myArrayList<ElemType>& other)

{

    this->mLen = other.mLen;

    this->mSize = other.mSize;

    this->mpArr = new  ElemType[this->mLen];

    for (int i = 0; i < this->mLen; i++)

        this->mpArr[i] = other.mpArr[i];

}
template<typename  ElemType>
myArrayList<ElemType>::myArrayList(int  n)

{

    this->mSize = n;

    this->mLen = 0;

    this->mpArr = new  ElemType[mSize];

}
template  <typename  ElemType>
void  myArrayList<ElemType>::show()

{

    for (int i = 0; i < mLen; i++)

        cout << mpArr[i] << "      ";//三个空格

    cout << endl;

}
template  <typename  ElemType>
ElemType  myArrayList<ElemType>::getMax()
{
    ElemType    max;
    max = mpArr[0];
    for (int i = 1; i < mLen; i++)
        if (max < mpArr[i])
            max = mpArr[i];
    return  max;
}
template <typename ElemType>
void myArrayList<ElemType>::sort() {
    ElemType n;
    int t;
    for (int i = 0; i < mLen; i++) {
        t = i;
        for (int j = i + 1; j < mLen; j++) {
            if (mpArr[t] > mpArr[j]) {
                t = j;
            }
        }
        if (i != t) {
            n = mpArr[i];
            mpArr[i] = mpArr[t];
            mpArr[t] = n;
        }
    }
}

//template<typename  ElemType>
//ostream& myArrayList<ElemType>::operator<<(ElemType& a) {
//    cout << a;
//    return cout;
//}
//Student.h

class  Student
{
private:
    int  mId;
    float  height;
    int  score;
public:
    Student(int  id = 0, float  h = 0, int  s = 0) :height(h), mId(id), score(s){}
    /*Student(Student& c):mId(c.mId),height(c.height),score(c.score) {}
    Student()*/
    friend class myArrayList<Student>;
};
template <>
void myArrayList<Student>::show() {
    for (int i = 0; i < mLen; i++) {
        cout << mpArr[i].mId << "      ";//三个空格
        cout << mpArr[i].height << "      ";
        cout << mpArr[i].score << "      "<<endl;
    }
}
template  <>
Student myArrayList<Student>::getMax()
{
    Student   max;
    max = mpArr[0];
    for (int i = 1; i < mLen; i++)
        if (max.score < mpArr[i].score)
            max.score = mpArr[i].score;
    return  max;
}
template <>
void myArrayList<Student>::sort() {
    Student n;
    int t;
    for (int i = 0; i < mLen; i++) {
        t = i;
        for (int j = i + 1; j < mLen; j++) {
            if (mpArr[t].score > mpArr[j].score) {
                t = j;
            }
        }
        if (i != t) {
            n = mpArr[i];
            mpArr[i] = mpArr[t];
            mpArr[t] = n;
        }
    }
}
template  <>
Student  myArrayList<Student>::getMax()
{
    Student  max;
    max = mpArr[0];
    for (int i = 1; i < mLen; i++)
        if (max.score < mpArr[i].score)
            max = mpArr[i];
    return  max;
}
//template  <>
//ostream& myArrayList<Student>::operator<<(Student &a) {
//    cout << a.score;
//    return cout;
//}
//主程序

int  main()
{
    int  a[] = { 1,  2,  3,  5,  7,  9,  12,  8 };
    double    b[] = { 1,  2.5,  3.6,  5,  7,  9,  12.8,  8 };
    myArrayList  <int>  list1(a, 8);
    list1.sort();
    list1.show();
    cout << "max=" << list1.getMax() << endl;
    myArrayList  <double>  list2(b, 8);
    list2.sort();
    list2.show();
    cout << "max=" << list2.getMax() << endl;
    Student  s[3] = { Student(1,  175,  80),  Student(2,  178,  90),  Student(3,  195,  83) }, s1;
    myArrayList  <Student>  list3(s, 3);
    list3.sort();
    list3.show();
    cout << "max=" << list3.getMax() << endl;
}
  • 写回答

3条回答 默认 最新

  • 关注

    (1)getMax()重复定义
    (2)student类没有重载<<运算符
    代码修改如下:

     
    #include  <iostream>
    #include  <iomanip>
    using  namespace  std;
    template  <typename  ElemType>
    class  myArrayList
    {
    private:
        int  mSize;
        int  mLen;
        ElemType* mpArr;
    public:
        myArrayList(int  n);
        myArrayList(ElemType* a, int  n);
        myArrayList(myArrayList<ElemType>& other);
        void  show();
        ElemType  getMax();
        void  sort();
        //ostream& operator<<(ElemType& a);
        //*************************
    };
    template<typename ElemType>
    myArrayList<ElemType>::myArrayList(ElemType* a, int n) :mpArr(a), mSize(n) {
        mLen = mSize;
    };
    template<typename  ElemType>
    myArrayList<ElemType>::myArrayList(myArrayList<ElemType>& other)
     
    {
     
        this->mLen = other.mLen;
     
        this->mSize = other.mSize;
     
        this->mpArr = new  ElemType[this->mLen];
     
        for (int i = 0; i < this->mLen; i++)
     
            this->mpArr[i] = other.mpArr[i];
     
    }
    template<typename  ElemType>
    myArrayList<ElemType>::myArrayList(int  n)
     
    {
     
        this->mSize = n;
     
        this->mLen = 0;
     
        this->mpArr = new  ElemType[mSize];
     
    }
    template  <typename  ElemType>
    void  myArrayList<ElemType>::show()
     
    {
     
        for (int i = 0; i < mLen; i++)
     
            cout << mpArr[i] << "      ";//三个空格
     
        cout << endl;
     
    }
    template  <typename  ElemType>
    ElemType  myArrayList<ElemType>::getMax()
    {
        ElemType    max;
        max = mpArr[0];
        for (int i = 1; i < mLen; i++)
            if (max < mpArr[i])
                max = mpArr[i];
        return  max;
    }
    template <typename ElemType>
    void myArrayList<ElemType>::sort() {
        ElemType n;
        int t;
        for (int i = 0; i < mLen; i++) {
            t = i;
            for (int j = i + 1; j < mLen; j++) {
                if (mpArr[t] > mpArr[j]) {
                    t = j;
                }
            }
            if (i != t) {
                n = mpArr[i];
                mpArr[i] = mpArr[t];
                mpArr[t] = n;
            }
        }
    }
     
    //template<typename  ElemType>
    //ostream& myArrayList<ElemType>::operator<<(ElemType& a) {
    //    cout << a;
    //    return cout;
    //}
    //Student.h
     
    class  Student
    {
    private:
        int  mId;
        float  height;
        int  score;
    public:
        Student(int  id = 0, float  h = 0, int  s = 0) :height(h), mId(id), score(s){}
    
        
        /*Student(Student& c):mId(c.mId),height(c.height),score(c.score) {}
        Student()*/
        friend class myArrayList<Student>;
    
        friend ostream & operator<<( ostream & os,const Student & c);//修改 重载<<运算符
    };
    
    ostream & operator<<( ostream & os,const Student & c)
    {
        os << c.mId<<"   "<<c.height<<"   "<<c.score<<endl;
        return os;
    }
    
    template <>
    void myArrayList<Student>::show() {
        for (int i = 0; i < mLen; i++) {
            cout << mpArr[i].mId << "      ";//三个空格
            cout << mpArr[i].height << "      ";
            cout << mpArr[i].score << "      "<<endl;
        }
    }
    template  <>
    Student myArrayList<Student>::getMax()
    {
        Student   max;
        max = mpArr[0];
        for (int i = 1; i < mLen; i++)
            if (max.score < mpArr[i].score)
                max.score = mpArr[i].score;
        return  max;
    }
    template <>
    void myArrayList<Student>::sort() {
        Student n;
        int t;
        for (int i = 0; i < mLen; i++) {
            t = i;
            for (int j = i + 1; j < mLen; j++) {
                if (mpArr[t].score > mpArr[j].score) {
                    t = j;
                }
            }
            if (i != t) {
                n = mpArr[i];
                mpArr[i] = mpArr[t];
                mpArr[t] = n;
            }
        }
    }
    //修改,重复定义,注释掉
    /*template  <>
    Student  myArrayList<Student>::getMax()
    {
        Student  max;
        max = mpArr[0];
        for (int i = 1; i < mLen; i++)
            if (max.score < mpArr[i].score)
                max = mpArr[i];
        return  max;
    }*/
    //template  <>
    //ostream& myArrayList<Student>::operator<<(Student &a) {
    //    cout << a.score;
    //    return cout;
    //}
    //主程序
     
    int  main()
    {
        int  a[] = { 1,  2,  3,  5,  7,  9,  12,  8 };
        double    b[] = { 1,  2.5,  3.6,  5,  7,  9,  12.8,  8 };
        myArrayList  <int>  list1(a, 8);
        list1.sort();
        list1.show();
        cout << "max=" << list1.getMax() << endl;
        myArrayList  <double>  list2(b, 8);
        list2.sort();
        list2.show();
        cout << "max=" << list2.getMax() << endl;
        Student  s[3] = { Student(1,  175,  80),  Student(2,  178,  90),  Student(3,  195,  83) }, s1;
        myArrayList  <Student>  list3(s, 3);
        list3.sort();
        list3.show();
        cout << "max=" << list3.getMax() << endl;
        return 0;
    }
    
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论 编辑记录
查看更多回答(2条)

报告相同问题?

问题事件

  • 系统已结题 4月5日
  • 已采纳回答 3月28日
  • 创建了问题 3月28日

悬赏问题

  • ¥20 软件测试决策法疑问求解答
  • ¥15 win11 23H2删除推荐的项目,支持注册表等
  • ¥15 matlab 用yalmip搭建模型,cplex求解,线性化处理的方法
  • ¥15 qt6.6.3 基于百度云的语音识别 不会改
  • ¥15 关于#目标检测#的问题:大概就是类似后台自动检测某下架商品的库存,在他监测到该商品上架并且可以购买的瞬间点击立即购买下单
  • ¥15 神经网络怎么把隐含层变量融合到损失函数中?
  • ¥15 lingo18勾选global solver求解使用的算法
  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行
  • ¥20 测距传感器数据手册i2c
  • ¥15 RPA正常跑,cmd输入cookies跑不出来