重载运算符类型不匹配

问题描述

重载运算符类型不匹配

#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.将日期转化为天数
" << "2.两日期相差天数
" << "3.计算所加天数后日期
" << "4.计算所减天数后日期
" << "5.将天数转化为日期
"
             << "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++学习Datemain.cpp||In function 'int main()':|
D:C++学习Datemain.cpp|65|error: no match for 'operator<<' in 'std::cout << date::operator-(const int&)(((const int&)((const int*)(& k))))'|
d:codeblocksmingwin..libgccmingw324.4.1includec++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:codeblocksmingwin..libgccmingw324.4.1includec++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:codeblocksmingwin..libgccmingw324.4.1includec++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:codeblocksmingwin..libgccmingw324.4.1includec++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:codeblocksmingwin..libgccmingw324.4.1includec++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:codeblocksmingwin..libgccmingw324.4.1includec++ostream|173|note:                 std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(bool) [with _CharT = char, _Traits = std::char_traits<char>]|
d:codeblocksmingwin..libgccmingw324.4.1includec++itsostream.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:codeblocksmingwin..libgccmingw324.4.1includec++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:codeblocksmingwin..libgccmingw324.4.1includec++itsostream.tcc|105|note:                 std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(int) [with _CharT = char, _Traits = std::char_traits<char>]|
d:codeblocksmingwin..libgccmingw324.4.1includec++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:codeblocksmingwin..libgccmingw324.4.1includec++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:codeblocksmingwin..libgccmingw324.4.1includec++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:codeblocksmingwin..libgccmingw324.4.1includec++ostream|209|note:                 std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(double) [with _CharT = char, _Traits = std::char_traits<char>]|
d:codeblocksmingwin..libgccmingw324.4.1includec++ostream|213|note:                 std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(float) [with _CharT = char, _Traits = std::char_traits<char>]|
d:codeblocksmingwin..libgccmingw324.4.1includec++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:codeblocksmingwin..libgccmingw324.4.1includec++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:codeblocksmingwin..libgccmingw324.4.1includec++itsostream.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++学习Datedat.h|21|note:                 std::ostream& operator<<(std::ostream&, date&)|
||=== Build failed: 1 error(s), 0 warning(s) (0 minute(s), 0 second(s)) ===|

解决方案

C++ Primer : : 第十四章 : 重载运算符与类型转换之类型转换运算符和重载匹配

解决方案二:

http://www.it165.net/pro/html/201408/19766.html

解决方案三:

我这里可以跑的啊,楼主要不贴一下错误?

时间: 2025-01-29 08:12:55

重载运算符类型不匹配的相关文章

link中运算符重载必须依附于类型这个怎么理解?什么是运算符重载的类型?

问题描述 link中运算符重载必须依附于类型这个怎么理解?什么是运算符重载的类型? link中运算符重载必须依附于类型这个怎么理解?什么是运算符重载的类型? 解决方案 就是说,C++允许在类的定义外面定义运算符重载.你可以重载两个整数的加减.但是C#不允许,你不能改变现有类型的运算符,只能给你自己定义的类型重载运算符.

《21天学通C++(第7版)》——第12章 运算符类型与运算符重载

第12章 运算符类型与运算符重载 21天学通C++(第7版) 关键字class让您不仅能够封装数据和方法,还能封装运算符,以简化对对象执行的操作.通过使用这些运算符,可以像第5章处理整数那样,对对象执行赋值或加法运算.与函数一样,运算符也可以重载. 在本章中,您将学习: 使用关键字operator: 单目运算符与双目运算符: 转换运算符: C++11新增的移动复制运算符: 不能重新定义的运算符. 本文仅用于学习和交流目的,不代表异步社区观点.非商业转载请注明作译者.出处,并保留本文的原始链接.

【C/C++学院】0820-Nullptr/const对象/类指针引用以及mallocfree与newde/类重载运算符/QT加法重载/类的重载赋值运算/自增在前在后差别/赋值重载深浅拷贝/重载下标

Nullptr #include<iostream> void go(int num) { std::cout << "gonum" << std::endl; } void go(void *p) { std::cout << "gop" << std::endl; } void main() { //void *p = nullptr; void *p = NULL;//C++是强类型,严格的类型检查

C++重载运算符的规则详解_C 语言

(1)C++不允许用户自己定义新的运算符,只能对已有的C++运算符进行重载.例如,有人觉得BASIC中用"* *"作为幂运算符很方便,也想在C++中将"* *"定义为幂运算符,用"3* *5"表示35,这是不行的. (2)C++允许重载的运算符C++中绝大部分运算符都是可以被重载的. 不能重载的运算符只有5个: .             (成员访问运算符) .*            (成员指针访问运算符) ::             (域运

link能不能重载==运算符?如果我想让字符串忽略大小写,怎么写?

问题描述 link能不能重载==运算符?如果我想让字符串忽略大小写,怎么写? link能不能重载==运算符?如果我想让字符串忽略大小写,怎么写? 解决方案 (1)不能,你需要重写Equals方法 (2)这个简单,比较的双方都转换成大写或者小写中的一种就可以了.toupper tolower

matrix-C++类中包含数组,如何重载运算符,第一次进入,还没有c币,求大神帮忙了,感激不尽

问题描述 C++类中包含数组,如何重载运算符,第一次进入,还没有c币,求大神帮忙了,感激不尽 这是头文件 #include #ifndef MATRIX_H_ #define MATRIX_H_ class matrix { private: double a[20][20]; int m,n; public: matrix(); matrix(const matrix&a); ~matrix(); matrix operator=(const matrix&a); friend void

C++重载运算符“&amp;amp;lt;&amp;amp;lt;”和“&amp;amp;gt;&amp;amp;gt;”出现错误

问题描述 C++重载运算符"<<"和">>"出现错误 主函数中用<<>时出现错误no operator defined which takes a right-hand operand of type 'class vsctor' (or there is no acceptable conversion) #includeusing namespace std;class vsctor{public: vsctor(int

oracle我是哪里的类型不匹配?

问题描述 oracle我是哪里的类型不匹配? 以下是代码!1 declare 2 type empcursor is ref cursor; 3 emp1 empcursor; 4 avgSal number; 5 deptno number; 6 empid number; 7 empSalary number; 8 begin 9 deptno:=&deptno;10 if deptno=2 then11 select avg(salary) into avgSal from emp whe

JAVA类型和SQL类型的匹配

已动态创建表为例 (1)    public class DataType {      private int code;      private String SQLType;      private String localType = null;      private String params = null;      private boolean needsSetting = true;       public DataType(int code, String SQLT