C++ 为什么我的计算结果不正确?一个工作日应等于8小时。

C++ 为什么我的计算结果不正确?一个工作日应等于8小时。,c++,class,c++11,object,operator-overloading,C++,Class,C++11,Object,Operator Overloading,我认为我的数学在setter函数中做得不对。我盯着它看太久了。提前感谢程序员 #include <iostream> using namespace std; class numDays { int hours; int days; public: numDays(int hrs); //constructor prototype void setHours(int); void setDays(int); int getHours(); int getDays();

我认为我的数学在setter函数中做得不对。我盯着它看太久了。提前感谢程序员

#include <iostream>
using namespace std;

class numDays
{
int hours;  
int days;

public:
numDays(int hrs); //constructor prototype
void setHours(int);
void setDays(int);
int  getHours();
int  getDays();

double operator+ (const numDays Object1) //overloading the + operator to   return the sum of two objects' hours members
{
return hours + Object1.hours;
}

double operator-(const numDays Object1) //overloading the + operator to     return the difference of two objects' hours members
{
return hours - Object1.hours;
}

numDays operator++() //prefix increment operator to increment # of hours   stored in object. Days are recalculated.
{
++hours;
days = hours/8;
return *this;
}

numDays operator++(int) //postfix increment operator to increment # of hours stored in object. Days are recalculated.
{
numDays temp(hours);
hours++;
days = hours/8;
return temp;
}

numDays operator--() //prefix decrement operator to decrement # of hours stored in object. Days are recalculated.
{
--hours;
days = hours/8;
return *this;
}

numDays operator-- (int) //prefix decrement operator to decrement # of     hours stored in object. Days are recalculated.
{
numDays temp(hours);
hours--;
days = hours/8;
return temp;
}

};

numDays::numDays(int hrs) //constructor that accepts a number of hours
{hours = hrs;}
#包括
使用名称空间std;
班级天数
{
整小时;
国际日;
公众:
numDays(int-hrs);//构造函数原型
无效设置小时数(int);
无效设置天数(int);
int getHours();
int getDays();
双运算符+(const numDays Object1)//重载+运算符以返回两个对象的小时数成员之和
{
返回小时数+1.5小时;
}
双运算符-(const numDays Object1)//重载+运算符以返回两个对象的小时数成员之差
{
返回时间-Object1.5小时;
}
numDays操作符+++()//将increment操作符前缀为对象中存储的小时数的增量。重新计算天数。
{
++小时数;
天=小时/8;
归还*这个;
}
numDays运算符++(int)//后缀增量运算符,用于增加存储在对象中的小时数。重新计算天数。
{
天数温度(小时);
小时++;
天=小时/8;
返回温度;
}
numDays运算符--()//前缀递减运算符以递减对象中存储的小时数。重新计算天数。
{
--小时数;
天=小时/8;
归还*这个;
}
numDays运算符--(int)//前缀递减运算符以递减对象中存储的小时数。重新计算天数。
{
天数温度(小时);
小时--;
天=小时/8;
返回温度;
}
};
numDays::numDays(int-hrs)//接受小时数的构造函数
{小时=小时;}
我想问题出在setHours函数或setDays函数中

void numDays::setHours(int hrs) //mutator function to store the amount of  hours
{
hours = hrs;
days = hrs/8;
}

void numDays::setDays(int d) //mutator function to store the amount of days
{
hours = d;
days = hours % 8;
}

int numDays::getHours() //accessor function to get the amount of hours
{
return hours;
}

int numDays::getDays() //accessor function to get the amount of days
{
return days;
}


int main()
{
int workHours;

numDays object2(0);

cout << "Please type in a certain amount of hours to see how much work in  days it is: ";
cin >> workHours;

object2.setHours(workHours);
object2.setDays(workHours);

cout << "The number of hours you put in is " << object2.getHours() << endl;
cout << "That means you worked " <<object2.getDays() << " days " << endl;

return 0;
}
void numDays::setHours(int hrs)//mutator函数用于存储小时数
{
小时=小时;
天数=小时/8;
}
void numDays::setDays(int d)//用于存储天数的mutator函数
{
小时=d;
天=小时%8;
}
int numDays::getHours()//获取小时数的访问器函数
{
返程时间;
}
int numDays::getDays()//用于获取天数的访问器函数
{
返程天数;
}
int main()
{
国际工作时间;
numDays object2(0);
工作时间;
目标2.设定时间(工作时间);
对象2.设定天数(工作时间);

cout也许我没有正确理解setDays()的作用,但这似乎是您想要的:

void numDays::setDays(int d) //mutator function to store the amount of days
{
    days = d;
    hours = days * 8;
}

假设
d
是天,那么
hours=d*8
;问题不清楚,范围太广。