本文介绍了使用运算符重载的日期操作.....的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经编写了一个程序来执行日期操作,以便:

_____________________________________________________________________

假设:



日期d1( 19  6  2014  //   d1是带参数的对象 
日期d1 = d1- 1 ;
cout<< d1;



然后它应该打印18/06/2014



_______________________________________________________________________



但是我得到的错误是:从'int'转换为非标量类型`Date''



我试过但我无法得到......请帮忙....



_______________________________________________________________________

代码:

======



 # include   <   iostream  >  
使用 命名空间性病;
class 日期
{
int 天;
int 月;
int 年;
public
Date( int d, int m, int y)
{
day = d;
month = m;
年= y;
}

朋友 int 运算符 - (日期&, int );
朋友 int 运算符+(日期&, INT );
void display()
{
cout<< 日期:<< day<< - <<月<< - << year< ;< ENDL;
}
};
int operator - (日期& x, int y)
{
return x.day-y;
}
int 运算符+(日期& y, int z)
{
return y.day-z;
}
int main()
{
int dd,mm,yy;
cout<< 输入日期:;
cin>> dd;
cout<< 输入月份:;
cin>> mm;
cout<< 输入年份:;
cin>> yy;

日期d1(dd,mm,yy);
日期d2 = d1- 1 ;
d1.display();
日期d3 = d1 + 1;
d1.display();
return 0 ;
}
解决方案



I have written a program to perform operation on date such that:
_____________________________________________________________________
Suppose:

Date d1(19,6,2014) //d1 is object with arguments
Date d1=d1-1;
cout<<d1;


then it should print 18/06/2014

_______________________________________________________________________

But I am getting en error that : " conversion from `int' to non-scalar type `Date' requested "

I have tried but i could not get...please help....

_______________________________________________________________________
Code:
======

#include<iostream>
using namespace std;
class Date
{
      int day;
      int month;
      int year;
      public:
             Date(int d,int m,int y)
             {
                   day=d;
                   month=m;
                   year=y;
             }
             
             friend int operator-(Date &,int);
             friend int operator+(Date &,int);
             void display()
             {
                  cout<<"Date:"<<day<<"-"<<month<<"-"<<year<<endl;
             }
};
int operator-(Date &x,int y)
{
     return x.day-y;
}
int operator+(Date &y,int z)
{
     return y.day-z;
}
int main()
{
    int dd,mm,yy;
    cout<<"Enter Day:";
    cin>>dd;
    cout<<"Enter Month:";
    cin>>mm;
    cout<<"Enter Year:";
    cin>>yy;
    
    Date d1(dd,mm,yy);
    Date d2=d1-1;
    d1.display();
    Date d3=d1+1;
    d1.display();
    return 0;
}
解决方案



这篇关于使用运算符重载的日期操作.....的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-24 10:52