qq_33627579 于 2016.05.09 21:36 提问

#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)) ===|
``````

3个回答

CSDNXIAON   2016.05.09 21:42

C++ Primer : : 第十四章 : 重载运算符与类型转换之类型转换运算符和重载匹配
----------------------同志你好，我是CSDN问答机器人小N，奉组织之命为你提供参考答案，编程尚未成功，同志仍需努力！

devmiao      2016.05.09 22:29
qq_33627579 我程序跑不起来，报没有匹配的《运算符，但是我已经重载了啊，我不知道哪里有歧义啊。。。

qq423399099      2016.05.10 09:36

qq423399099 回复荆棘花王朝: 双击错误显示的是哪一行，重新生成工程试试，或者重建工程

qq_33627579 用的Code Blocks。。。

qq423399099 回复荆棘花王朝: 楼主用的什么IDE？是手动编译的？

qq_33627579 我随便截取了一段，在上边

java 数组元素类型不匹配异常（ArrayStoreException）
java 数组元素类型不匹配异常（ArrayStoreException） java 数组元素类型不匹配异常（ArrayStoreException）

// ex5-11.cpp : 定义控制台应用程序的入口点。 // #include "stdafx.h" #include #include using namespace std; int _tmain(int argc, _TCHAR* argv[]) { string word="?ate"; for (char ch='a';word!="mate";++ch) { c

C++中Cout<<的运算符重载后优先级问题
C++中的运算符重载，切记一点，自定义类的运算符重载函数也是函数，自己重载的运算符的优先级不会因为是自己定义的而改变，依然遵循与内部运算符一样的优先级运算顺序！ 在论坛看到一个提问，报错的地方是这样的： int A = 0; cout 前边的代码都可以忽略，没有任何问题，这里会有错误，“表达式必须包含整数或枚举类型”。我们都知道的VS的报错跟错误没有一点关系，忽略不管它。这里稍微改一

【1】 bool operator > (const Book &p) const { return p.isbn < isbn; } 是对运算符 > 的重载 ，const Book &p 是 对类Book 的引用 &p，p.isbn 【2】 在STL中，如果要将自定义的类型放入到set中的话，就需要重载“ 假设现在设计如下类型： 这时，为了让MyType类型可以顺利地放进s

【C++】重载运算符号时的函数返回值类型问题
c++课本p312有关于单目运算符++重载的示例 以前++为例 其代码Time Time::operator++(){ if(++sec>=60){ sec-=60; ++minute; } return *this;}//看到之后非常不解，此程序明明只需要将本对象中的private数据minute和second改掉就好了，就如同我的版本void
Access合并表 类型不匹配问题的解决方式