#ifndef DAT_H_INCLUDED
#define DAT_H_INCLUDED
#include
#include
#include
using namespace std;
class date
{
private:
int year;
int month;
int day;
int dton(date &a); //将指定日期转换成从0年O月O日起的天数
date ntod(int b); //将指定的0年O月O日起的天数转换成对应的日期
bool leap(int year);
public:
date();
date(int y,int m,int d);
friend istream &operator >> (istream &is,date &d);
friend ostream &operator << (ostream &os,date &d);
date operator +(int &days);
date operator -(int &days);
int operator -(date &da);
void operator =(date &da);
int dt(date &a);
date nt(int b);
bool le(int year);
~date();
};
#endif // DAT_H_INCLUDED
#include"dat.h"
#include<istream>
#include<ostream>
#include<iostream>
using namespace std;
static int day_tab[2][12] = {{31,28,31,30,3l,30,3l,3l,30,31,30,31},
{31,29,31,30,31,30,31,31,30,31,30,31}};
date::date()
{
year = 0;
month = 1;
day = 1;
}
date::date(int y,int m,int d)
{
year = y;
month = m;
day = d;
}
istream &operator >> (istream &is,date &d)
{
cin >> d.year >> d.month >> d.day;
return is;
}
ostream &operator << (ostream &os,date &d)
{
cout << d.year << "." << d.month << "." << d.day << endl;
return os;
}
bool date::leap(int year)
{
if((year % 4 == 0 && year % 100 != 0)||year % 400 == 0)
return true;
else
return false;
}
date date::operator +(int &days)
{
int rest;
rest = this->dt(*this) + days;
return this->nt(rest);
}
int date::dton(date &a)
{
int ye,mo,da=0;
for(ye = 0;ye < a.year;ye++)
{
if(a.le(a.year))
da += 366;
else da += 365;
}
for(mo = 0;mo < a.month-1;mo++)
{
if(a.le(a.year))
da += day_tab[0][mo];
else da += day_tab[1][mo];
}
da += a.day;
return da;
}
date date::ntod(int b)
{
date a(0,1,1);
int rest = b;
while(1)
{
if(a.le(a.year))
{
rest -= 366;
if(rest < 0)
{
rest += 366;
break;
}
a.year++;
}
else
{
rest -= 365;
if(rest < 0)
{
rest += 365;
break;
}
a.year++;
}
}
while(1)
{
if(a.le(a.year))
{
for(int i = 0;i < 12;i++)
{
rest -= day_tab[0][i];
if(rest < 0)
{
rest += day_tab[0][i];
break;
}
a.month++;
}
}
else
{
for(int i = 0;i < 12;i++)
{
rest -= day_tab[1][i];
if(rest < 0)
{
rest += day_tab[1][i];
break;
}
a.month++;
}
}
break;
}
a.day = rest;
return a;
}
date date::operator -(int &days)
{
int da;
da = this->dt(*this) - days;
return this->nt(da);
}
int date::operator -(date &da)
{
int rest;
rest = this->dt(*this) - da.dt(da);
return rest;
}
int date::dt(date &a)
{
return dton(a);
}
date date::nt(int b)
{
return ntod(b);
}
bool date::le(int year)
{
return leap(year);
}
void date::operator =(date &da)
{
this->year = da.year;
this->month = da.month;
this->day = da.day;
}
date::~date()
{
}
#include<iostream>
#include<ostream>
#include<windows.h>
#include"dat.h"
using namespace std;
int main()
{
while(1)
{
date *p = new date;
cout << "请输入日期:" << endl;
cin >> *p;
int n;
cout << "请选择功能:" << endl;
cout << "1.将日期转化为天数\n" << "2.两日期相差天数\n" << "3.计算所加天数后日期\n" << "4.计算所减天数后日期\n" << "5.将天数转化为日期\n"
<< "6.退出" << endl;
cin >> n;
while(n<1||n>6)
{
cout << "输入错误,请重新选择功能" << endl;
n = 0;
cin >> n;
}
system("cls");
switch(n)
{
case 1:
{
cout << p->dt(*p) << endl;
system("pause");
system("cls");
break;
}
case 2:
{
date *q = new date;
cout << "请输入日期:" << endl;
cin >> *q;
cout << *p - *q << endl;
system("pause");
system("cls");
delete q;
break;
}
case 3:
{
cout << "请输入天数:" << endl;
int m;
cin >> m;
cout << *p + m << endl;
system("pause");
system("cls");
break;
}
case 4:
{
cout << "请输入天数:" << endl;
int k;
cin >> k;
cout << (*p - k) << endl;
system("pause");
system("cls");
break;
}
case 5:
{
cout << "请输入天数:" << endl;
int u;
cin >> u;
date q = p->nt(u);
cout << q << endl;
system("pause");
system("cls");
break;
}
case 6:exit(0);
}
}
}
报错:
||=== Build: Debug in Date (compiler: GNU GCC Compiler) ===|
D:\C++学习\Date\main.cpp||In function 'int main()':|
D:\C++学习\Date\main.cpp|65|error: no match for 'operator<<' in 'std::cout << date::operator-(const int&)(((const int&)((const int*)(& k))))'|
d:\codeblocks\mingw\bin\..\lib\gcc\mingw32\4.4.1\include\c++\ostream|108|note: candidates are: std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(std::basic_ostream<_CharT, _Traits>& (*)(std::basic_ostream<_CharT, _Traits>&)) [with _CharT = char, _Traits = std::char_traits<char>]|
d:\codeblocks\mingw\bin\..\lib\gcc\mingw32\4.4.1\include\c++\ostream|117|note: std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(std::basic_ios<_CharT, _Traits>& (*)(std::basic_ios<_CharT, _Traits>&)) [with _CharT = char, _Traits = std::char_traits<char>]|
d:\codeblocks\mingw\bin\..\lib\gcc\mingw32\4.4.1\include\c++\ostream|127|note: std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(std::ios_base& (*)(std::ios_base&)) [with _CharT = char, _Traits = std::char_traits<char>]|
d:\codeblocks\mingw\bin\..\lib\gcc\mingw32\4.4.1\include\c++\ostream|165|note: std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(long int) [with _CharT = char, _Traits = std::char_traits<char>]|
d:\codeblocks\mingw\bin\..\lib\gcc\mingw32\4.4.1\include\c++\ostream|169|note: std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(long unsigned int) [with _CharT = char, _Traits = std::char_traits<char>]|
d:\codeblocks\mingw\bin\..\lib\gcc\mingw32\4.4.1\include\c++\ostream|173|note: std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(bool) [with _CharT = char, _Traits = std::char_traits<char>]|
d:\codeblocks\mingw\bin\..\lib\gcc\mingw32\4.4.1\include\c++\bits\ostream.tcc|91|note: std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(short int) [with _CharT = char, _Traits = std::char_traits<char>]|
d:\codeblocks\mingw\bin\..\lib\gcc\mingw32\4.4.1\include\c++\ostream|180|note: std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(short unsigned int) [with _CharT = char, _Traits = std::char_traits<char>]|
d:\codeblocks\mingw\bin\..\lib\gcc\mingw32\4.4.1\include\c++\bits\ostream.tcc|105|note: std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(int) [with _CharT = char, _Traits = std::char_traits<char>]|
d:\codeblocks\mingw\bin\..\lib\gcc\mingw32\4.4.1\include\c++\ostream|191|note: std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(unsigned int) [with _CharT = char, _Traits = std::char_traits<char>]|
d:\codeblocks\mingw\bin\..\lib\gcc\mingw32\4.4.1\include\c++\ostream|200|note: std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(long long int) [with _CharT = char, _Traits = std::char_traits<char>]|
d:\codeblocks\mingw\bin\..\lib\gcc\mingw32\4.4.1\include\c++\ostream|204|note: std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(long long unsigned int) [with _CharT = char, _Traits = std::char_traits<char>]|
d:\codeblocks\mingw\bin\..\lib\gcc\mingw32\4.4.1\include\c++\ostream|209|note: std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(double) [with _CharT = char, _Traits = std::char_traits<char>]|
d:\codeblocks\mingw\bin\..\lib\gcc\mingw32\4.4.1\include\c++\ostream|213|note: std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(float) [with _CharT = char, _Traits = std::char_traits<char>]|
d:\codeblocks\mingw\bin\..\lib\gcc\mingw32\4.4.1\include\c++\ostream|221|note: std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(long double) [with _CharT = char, _Traits = std::char_traits<char>]|
d:\codeblocks\mingw\bin\..\lib\gcc\mingw32\4.4.1\include\c++\ostream|225|note: std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(const void*) [with _CharT = char, _Traits = std::char_traits<char>]|
d:\codeblocks\mingw\bin\..\lib\gcc\mingw32\4.4.1\include\c++\bits\ostream.tcc|119|note: std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(std::basic_streambuf<_CharT, _Traits>*) [with _CharT = char, _Traits = std::char_traits<char>]|
D:\C++学习\Date\dat.h|21|note: std::ostream& operator<<(std::ostream&, date&)|
||=== Build failed: 1 error(s), 0 warning(s) (0 minute(s), 0 second(s)) ===|