-2
私は自分の本をTに従っています。何らかの理由で自分のプログラムを実行しようとすると、演算子と演算子の出力が完全に間違って出力されます。私のオーバーロードされた演算子と私のオーバーロードされた演算子+で何がうまくいかないかを知っていますか?プログラムはうまくコンパイルされますが、出力はまったく正しくありません。算術演算子と関係演算子
#include <iostream>
using namespace std;
class NumDays
{
private:
int ptrHours;
public:
NumDays(int H)// to set the pointer
{ setHours(H);}
void setHours(int H)
{ptrHours = H;}
int gethours() {return ptrHours;}
double calcDays()// function to calculate the days
{
double days;
days = ptrHours/8.0;
return days;
}
friend NumDays operator+(NumDays a, NumDays b);
friend NumDays operator-(NumDays a, NumDays b);
};
NumDays operator+(NumDays a, NumDays b)
{
return NumDays(a.ptrHours + b.ptrHours);
}
NumDays operator-(NumDays a, NumDays b)
{
return (a.ptrHours - b.ptrHours);
}
int main()
{
NumDays first(0),
second(0),
third(0);
int hours1, hours2;
cout <<"Enter the how many hours you worked..." << endl;
cout <<"First set: ";
cin >> hours1;
while (hours1 < 0)
{
cout <<"\nYou cannot enter a negative value. " << endl;;
cin >> hours1;
}
first.setHours(hours1);
cout <<"Second Set: ";
cin >> hours2;
while (hours1 < 0)
{
cout <<"\nYou cannot enter a negative value. " << endl;;
cin >> hours2;
}
second.setHours(hours2);
cout <<"First set for days worked is " << first.calcDays() <<" days." << endl;
cout <<"Second set for days worked is " << second.calcDays() <<" days." << endl;
third = first - second;// where I try and do my arithmetic operators
cout <<"First - Second = " << cout << third.gethours() << endl;
third = first + second;
cout <<"First + Second = " << cout << third.gethours() << endl;
cin.ignore();
cin.get();
return 0;
}
あなたはどんな出力を得ていますか?何を期待しましたか?あなたの質問を編集してください(実際の出力をコピーして質問本体に貼り付けてください)。 –
フォーマットしてください。また、[コンパイルしないでください](http://coliru.stacked-crooked.com/a/bb217e190d2df2ff)。 – LogicStuff
また、問題の再現に必要でない例の部分を削除してください。 – user2079303