#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;
}
模板的参数变为另一个类后,cout<<没有匹配了
- 写回答
- 好问题 0 提建议
- 追加酬金
- 关注问题
- 邀请回答
-
3条回答 默认 最新
- 技术专家团-小桥流水 2022-03-28 22:18关注
(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; }
本回答被题主选为最佳回答 , 对您是否有帮助呢?解决 无用评论 打赏 举报 编辑记录
悬赏问题
- ¥20 wpf datagrid单元闪烁效果失灵
- ¥15 券商软件上市公司信息获取问题
- ¥100 ensp启动设备蓝屏,代码clock_watchdog_timeout
- ¥15 Android studio AVD启动不了
- ¥15 陆空双模式无人机怎么做
- ¥15 想咨询点问题,与算法转换,负荷预测,数字孪生有关
- ¥15 C#中的编译平台的区别影响
- ¥15 软件供应链安全是跟可靠性有关还是跟安全性有关?
- ¥15 电脑蓝屏logfilessrtsrttrail问题
- ¥20 关于wordpress建站遇到的问题!(语言-php)(相关搜索:云服务器)