求高手帮忙看一下下面的代码
#include "stdafx.h"#include<iostream>
#include<iomanip>
#include<cassert>
using namespace std;
class Date
{
int iMonth,iDay,iYear;
char format[128];
public:
Date(int m=0,int d=0,int y=0)
{
iMonth=m;
iDay=d;
iYear=y;
}
friend ostream& operator<<(ostream& os,const Date t)
{
cout<<"Month:"<<t.iMonth<<' ';
cout<<"Day:"<<t.iDay<<' ';
cout<<"Year:"<<t.iYear<<' ';
return os;
}
void Display()
{
cout<<"Month:"<<iMonth;
cout<<"Day:"<<iDay;
cout<<"Year:"<<iYear;
cout<<endl;
}
};
template<class T,int b>
class Array
{
public:T elem[b];
Array(){}
T& operator[](int sub)
{
assert(sub>=0&&sub<b);
return elem[sub];
}
};
int _tmain(int argc, _TCHAR* argv[])
{
Array<Date,3>dateArray;
Date dt1(1,2,3);
Date dt2(4,5,6);
Date dt3(7,8,9);
dateArray[0]=dt1;
dateArray[1]=dt2;
dateArray[2]=dt3;
for(int i=0;i<3;i++)
{
cout<<dateArray[i]<<endl;
}
Date dt4(10,11,13);
dateArray[3]=dt4;
cout<<dateArray[3]<<endl;
return 0;
}
定义的数组不该是dateArray.elemma[3]吗?为什么可以直接用dateArray[i]=值,而不是dateArray.elem[i]=值
[此贴子已经被作者于2019-8-2 16:50编辑过]