2016-04-26 11 views
-3

I Class NumDays haben, wie gezeigt:Falsche Ausgabe von überladenen Operator << C++

Class NumDays 
{ 
    private: 
     double hours; 

    public: 
     NumDays() { hours = 0.0; }   //default constructor 
     NumDays(double hr) { hr = hours; } //initializing constructor 

    //Large class, nothing of importance, rest of class omitted 

    //overloading << operator 
    friend ostream &operator<<(ostream &out, NumDays a); 
} 

Ich habe NumDay.cpp, der folgendes beinhaltet:

ostream &operator<<(ostream& out, NumDays a) 
{ 
    // takes amount of hours, computes to work days 

    int temp = a.hours/8; 

    //gives remainder of hours after full 8 hr workday. 

    double hrs = a.hours - (temp * 8); 

    //outputs 
    cout << fixed << setprecision(0); 
    out << (a.hours/8) << " Days, " << hrs << "hours"; 
    return out; 
} 

Und ich habe main.cpp enthalten:

int main() 
{ 
    // Initialized UDT object Declarations 
    NumDays hoursWorked_John;  // Instantiate with Default Constructor 
    NumDays hoursWorked_Sue(36.9); // Instantiate with Initializing Cons  
    NumDays hoursUsed_Sue(4.5);  // Instantiate with Initializing Cons 

    cout << "John's initial hours worked: " << hoursWorked_John << endl; 
    hoursWorked_John.addHours(56.78); 
    cout << " John's final hours worked: " << hoursWorked_John << endl; 

    cout << "Sue's initial hours worked: " << hoursWorked_Sue << endl; 

    //rest of main omitted for sake of size 

Wenn ich diesen kleinen Abschnitt des Programms ausführen, ist dies meine Konsole:

Consle Output

Irgendwelche Gedanken darüber, warum Stunden von Sue so phantastisch falsch sind, aber Johns sind richtig?

Antwort

7
NumDays(double hr) { hr = hours; } //initializing constructor 

Whoops. Hier lassen Sie das Mitglied hours nicht initialisiert und ändern das temporäre Argument hr. Sie scheinen

NumDays(double hr) { hours = hr; } 

(oder noch besser :)

NumDays(double hr) : hours(hr) {} 
+0

Solch ein dummer Fehler. Danke Freund :) – Podo

+0

War das eine unangemessene Frage zu stellen? Ich wurde 3 mal downvoted. Die Frage ist gültig, die Formatierung ist gut. Ich konnte den Fehler nicht finden, also hielt ich es für angebracht, ihn hierher zu bringen ... – Podo

2
NumDays(double hr) { hr = hours; } 

sollte

NumDays(double hr) { hours = hr; } 
0

Das Problem ist, dass Sie Müll Werte drucken zu bedeuten, weil Ihr Betreiber << Anrufe die Standardeinstellung copy constructor von NumDays, die nichts tut.

NumDays(const NumDays& other){ 
} 

Das bedeutet, dass Sie die Objekte nicht drucken, die Sie vorher initialisiert.
Lösung:
Ihre Betreiber Prototyp wie folgt ändern:

/* change NumDays a to const NumDays& a 
* a must not be modified by this operator (const). 
* a must be passed by reference (NumDays&) to avoid duplications (copy constructor) 
*/ 
ostream &operator<<(ostream& out,const NumDays & a) 
{ 
    int days = a.hours/8.f; 
    streamsize old_precision = out.precision(0); 
    ios_base::fmtflags old_flags = out.flags(); 
    out <<fixed<< days << " Days, " << (a.hours - days*8.f) << " hours"; 
    out.precision(old_precision);//restore old precision 
    out.flags(old_flags);//restore old format flags 
    return out; 
} 

Prototyp kann Ihre Betreiber << nur funktionieren, wenn die `copy constructor' wie diese überlastet ist:

NumDays(const NumDays& other){ 
    hours = other.hours; 
} 

nicht tun Ich glaube, Sie Notwendigkeit double Präzision float ist genug

Getestet mit: g++ (Debian 4.9.2-10) 4.9.2