2016-07-04 2 views
-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; 

} 
+4

출력이 얼마이며 예상 한 결과는 무엇입니까? 질문을 편집하여 포함 시키십시오 (실제 결과물을 질문 본문에 복사하여 붙여 넣으십시오). –

+0

서식을 지정하십시오. 또한 [컴파일하지 말아야] (http://coliru.stacked-crooked.com/a/bb217e190d2df2ff). – LogicStuff

+0

또한 예제를 재현 할 필요가없는 부분을 제거하십시오. – user2079303

답변

0

문제는 코드에 있습니다.

cout <<"First + Second = " << cout << third.gethours() << endl; 

귀뚜라미 안쪽에 귀뚜라미를 사용하지 마십시오. 귀하의 진술은 다음과 같아야합니다.

cout <<"First + Second = " << third.gethours() << endl; 

희망이 있으면 도움이 될 것입니다. 감사합니다.