RocketKatz
RocketKatz

Reputation: 39

c++ error within this context and overloading operator+

I'm working on this assignment and having trouble coding it. The function operator+ is the error within context. Also are all or some of my functions correct to be used for this assignment? If not, then please point out what should be corrected

The program makes a NumDays class using two int member variables to represent how many days and hours a person worked(A day is only 8 hours total). The program requires at the use of operator+ and the postfix and prefix operators of increment (++) and decrement (--). For example:

8 hours= 1 day

12 hours= 1 day and four hours

16 hours= 2 days

The operator+ function must call the private member function to convert to the correct number of days and hours.

The private member function should be called to convert to the correct number of days and hours.

If both the number of days and hours are already 0, the decrement operators should do nothing to the object. The private member function should be called to convert to the correct number of days and hours, if the object was changed.

EDIT

My functions including operator+ are working, but it isn't returning the correct number when I add both one and two objects. I'm getting 0 days and 2 hours. What should I be returning in the operator+?

EDIT 2

Finally, the operator+ is working. Replacing the first a.getDays() with this->day_hour + a.day_hour worked.

Here is my program so far:

#include <iostream>

using namespace std;

class NumDays
{
private:
    int day_hour;
public:
    NumDays(int, int);
    NumDays(int);
    void setTime(int, int);
    int getDays() const;
    int getHours() const;
    NumDays operator+(const NumDays&);
    NumDays operator++();
    NumDays operator++(int);
    NumDays operator--();
    NumDays operator--(int);
};
NumDays::NumDays(int days, int hours)
{
setTime(days, hours);
}
NumDays::NumDays(int hours)
{
day_hour=hours;
}
void NumDays::setTime(int days, int hours)
{
day_hour=8 *days+hours;
}
int NumDays::getDays() const
{
return day_hour/8;
}
int NumDays::getHours() const
{
return day_hour%8;
}
NumDays NumDays::operator+(const NumDays& a)
{
return NumDays(a.getDays()+ a.getHours());
}
NumDays NumDays::operator++()
{
day_hour++;
return *this;
}
NumDays NumDays::operator++(int)
{
NumDays time=*this;
day_hour++;
return time;
}
NumDays NumDays::operator--()
{
day_hour--;
return *this;
}
NumDays NumDays::operator--(int)
{
NumDays time=*this;
day_hour--;
return time;
}

int main()
{
NumDays one(25);
NumDays two(16);
NumDays three(0);
cout<<endl<<one.getDays()<<" days and "<<one.getHours()<<" hours."<<endl;
cout<<two.getDays()<<" days and "<<two.getHours()<<" hours."<<endl;
three=one+two;
cout<<three.getDays()<<" days and "<<three.getHours()<<" hours."<<endl;
cout<<endl;
for(int i=0; i<3; i++)
{
    one=++two;
    cout<<one.getDays()<<" days and "<<one.getHours()<<" hours."<<endl;
}
cout<<endl;
for(int i=0; i<3; i++)
{
    one=two++;
    cout<<one.getDays()<<" days and "<<one.getHours()<<" hours."<<endl;
}
cout<<endl;
for(int i=0; i<3; i++)
{
two=--one;
cout<<two.getDays()<<" days and "<<two.getHours()<<" hours."<<endl;
}
cout<<endl;
for(int i=0; i<3; i++)
{
two=one--;
cout<<two.getDays()<<" days and "<<two.getHours()<<" hours."<<endl;
}
return 0;
}

Upvotes: 1

Views: 416

Answers (1)

vsoftco
vsoftco

Reputation: 56557

The declaration of

NumDays operator+();

does not correspond to the definition

NumDays NumDays::operator+(NumDays a, NumDays b){...}

If you want to have operator+ a member function, it has to be declared as

NumDays operator+(const NumDays& rhs); // you invoke it on current instance

However it's better to declare binary operators not as member functions, but as friends, such as:

friend NumDays operator+(const NumDays& lhs, const NumDays& rhs);

The way you declare them implies that you are trying to invoke them on the current instance (in which case you'd only need one parameter, not two).

See Operator overloading for an excellent guide.

Upvotes: 3

Related Questions