模板的参数变为另一个类后,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;
}

(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;
}

重载<<运算符

您好,我是有问必答小助手,您的问题已经有小伙伴帮您解答,感谢您对有问必答的支持与关注!
PS:问答VIP年卡 【限时加赠:IT技术图书免费领】,了解详情>>> https://vip.csdn.net/askvip?utm_source=1146287632