c++,operator<<必须要一个返回值,解决比采纳?
#include<iostream>#include<assert.h>usingnamespacestd;classDate{inti_Month,i_Day,i_ye...
#include<iostream>
#include<assert.h>
using namespace std;
class Date
{
int i_Month, i_Day, i_year;
char Format[128];
public:
Date(int m, int d, int y) { i_Month = m, i_Day = d, i_year = y; }
friend ostream& operator<<(ostream& os, const Date t)
{
cout << "Month: " << t.i_Month;
cout << "Day: " << t.i_Day;
cout << "year: " << t.i_year;
cout << endl;
}
};
template <class T>
class Set
{
T t;
public:
Set(T ts):t(ts) {}
void display()
{
cout << t << endl;
}
};
template<>class Set<Date>
{
Date t;
public:
Set(Date st):t(st){}
void Display()
{
cout << "Date: " << t << endl;
}
};
int main()
{
Set<int>intSet(123);
Set<Date>dt = Date(1, 2, 3);
intSet.display();
dt.Display();
} 展开
#include<assert.h>
using namespace std;
class Date
{
int i_Month, i_Day, i_year;
char Format[128];
public:
Date(int m, int d, int y) { i_Month = m, i_Day = d, i_year = y; }
friend ostream& operator<<(ostream& os, const Date t)
{
cout << "Month: " << t.i_Month;
cout << "Day: " << t.i_Day;
cout << "year: " << t.i_year;
cout << endl;
}
};
template <class T>
class Set
{
T t;
public:
Set(T ts):t(ts) {}
void display()
{
cout << t << endl;
}
};
template<>class Set<Date>
{
Date t;
public:
Set(Date st):t(st){}
void Display()
{
cout << "Date: " << t << endl;
}
};
int main()
{
Set<int>intSet(123);
Set<Date>dt = Date(1, 2, 3);
intSet.display();
dt.Display();
} 展开
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询