Click here to Skip to main content
14,697,418 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
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;
}
Posted
Updated 19-Jun-14 22:29pm
v3
Comments
   
It simply means that you did not really implement proper operator overloading, or something, else. Do you really think there is something to look at, without showing any implementation?
—SA
A94 20-Jun-14 0:51am
   
I have implemented operator overloading using friend function where d1-/+ invokes the function and 1 is passed as an argument.IS copy constructor required...

The problem is in your Operator Overloading. You have declared return type int but you are using it as Date. Date d2=d1-1. Below is a way to help you understand the problem.

Date operator-(Date &x,int y)
{

return Date(x.day-y, x.month, x.year);

}
   
v2
Comments
A94 20-Jun-14 1:22am
   
Still getting en error that : Date should have zero or one argument"
Stefan_Lang 20-Jun-14 3:34am
   
try this:
Date operator-(Date& x, int y)
{
return Date(x.day-y, x.month, x.year);
}
Stefan_Lang 20-Jun-14 3:32am
   
You forgot the x. on month and year
_Asif_ 20-Jun-14 5:10am
   
Right, fixed the code, thanks! :)
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 Date operator -(Date &,int);
             friend Date operator +(Date &,int);
             void display()
             {
                  cout<<"Date:"<<day<<"-"<<month<<"-"<<year<<endl;
             }
};
Date operator-(Date &x,int y)
{
     return Date(x.day-y, x.month, x.year);
}
Date operator+(Date &x,int y)
{
     return Date(x.day+y, x.month, x.year);
}
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;
    d2.display();
    Date d3=d1+1;
    d3.display();
    return 0;
}


sample execution I/O:
Enter Day:20
Enter Month:6
Enter Year:2014
Date:19-6-2014
Date:21-6-2014
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900