2 qq 33627579 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
CSDNXIAON   2016.05.09 21:42
已采纳

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

devmiao
devmiao   Ds   Rxr 2016.05.09 22:29
qq_33627579
qq_33627579 我程序跑不起来,报没有匹配的《运算符,但是我已经重载了啊,我不知道哪里有歧义啊。。。
大约 2 年之前 回复
qq423399099
qq423399099   Ds   Rxr 2016.05.10 09:36

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

qq423399099
qq423399099 回复荆棘花王朝: 双击错误显示的是哪一行,重新生成工程试试,或者重建工程
大约 2 年之前 回复
qq_33627579
qq_33627579 用的Code Blocks。。。
大约 2 年之前 回复
qq423399099
qq423399099 回复荆棘花王朝: 楼主用的什么IDE?是手动编译的?
大约 2 年之前 回复
qq_33627579
qq_33627579 我随便截取了一段,在上边
大约 2 年之前 回复
Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!
其他相关推荐
java 数组元素类型不匹配异常(ArrayStoreException)
java 数组元素类型不匹配异常(ArrayStoreException) java 数组元素类型不匹配异常(ArrayStoreException)
重载运算符时的参数和返回类型要用引用的说明
一·我们举个必须要用重载运算符和重载运算符时必须要使用引用的例子 #include #pragma pack(8) using namespace std; class Myclass { private: char* str; public: Myclass(char* str1 = "default string") { str = new char[strlen(
利用string类重载运算符!=比较string类字符串
// 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++运算符重载返回类型的认识
虽然还是大一,但为了应对计算机等级考试...我看了近半个学期的C++...其中有不少不很明了的地方...比如运算符重载返回类型的问题...在这里归纳一下...主要写下返回类型的问题.1)返回引用:  1.相当于返回一个地址,如果返回类型的话,将会创建临时的对象,将会添加额外的消耗,就像返回指针一样.  2.返回引用可以不通过->来得到成员函数和变量,可以继续使用操作符.2)返回
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
运算符重载--函数返回值
举例 #include  using namespace std; class Test { public:   Test(int a = 0) {cout   Test(Test &temp){cout this class name: "name.c_str()   ~Test(){cout   friend Test operator + (
定义一个字符串类
我们知道在C++里不同于C,对于字符串如何表示有两种方式: 一种是把字符串作为字符数组来处理,即基于指针的字符串或称作C字符串:arr[]="city is good" 或者*arr={'c','i','t','y','\0'}; 一种是C++库里面提供的string类,可以利用它创建字符串,利用成员函数进行字符串,但是C++库只给出相应接口,如何实行已经封装好。为此我们可以不厌其烦研究如何实
【C++】重载运算符号时的函数返回值类型问题
c++课本p312有关于单目运算符++重载的示例 以前++为例 其代码Time Time::operator++(){ if(++sec>=60){ sec-=60; ++minute; } return *this;}//看到之后非常不解,此程序明明只需要将本对象中的private数据minute和second改掉就好了,就如同我的版本void
Access合并表 类型不匹配问题的解决方式
一朋友,目前与Access打交道的机会比较多,遇上了合并表的问题,我猜想应该是各级经销商需要做报表,汇总信息吧。问题是来自各级的Access表的字段类型设计的并不一致,导致在拷贝粘贴复制的时候出现了类型不匹配的错误。根据错误提示,我首先查看了一下各个表的字段类型,发现果然有一个字段在子表中的类型为文本,而总表的对应字段类型为数字(为方便说明,假设子表中字段名为temp_text)。这是导致错误