才开始学C++请问下面代码中Array 怎么调用friend ostream& operator<<(ostream& os, const Data t)的?
#include<iostream>#include<iomanip>#include<cassert>usingnamespacestd;classData{intiM...
#include<iostream>
#include<iomanip>
#include<cassert>
using namespace std;
class Data
{
int iMonth, iDay, iYear;
char Format[128];
public:
Data(int m = 0, int d = 0, int y = 0)
{
iMonth = m;
cout << iMonth << endl;
iDay = d;
iYear = y;
}
friend ostream& operator<<(ostream& os, const Data 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
{
T elem[b];
public:
Array(){}
T& operator[](int sub)
{
assert(sub >= 0 && sub < b);
return elem[sub];
}
};
void main()
{
Array<Data, 4> dataArray;
Data dt4(10, 11, 13);
dataArray[3] = dt4;
cout << dataArray[3] << endl;
} 展开
#include<iomanip>
#include<cassert>
using namespace std;
class Data
{
int iMonth, iDay, iYear;
char Format[128];
public:
Data(int m = 0, int d = 0, int y = 0)
{
iMonth = m;
cout << iMonth << endl;
iDay = d;
iYear = y;
}
friend ostream& operator<<(ostream& os, const Data 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
{
T elem[b];
public:
Array(){}
T& operator[](int sub)
{
assert(sub >= 0 && sub < b);
return elem[sub];
}
};
void main()
{
Array<Data, 4> dataArray;
Data dt4(10, 11, 13);
dataArray[3] = dt4;
cout << dataArray[3] << endl;
} 展开
1个回答
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询