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:
Irgendwelche Gedanken darüber, warum Stunden von Sue so phantastisch falsch sind, aber Johns sind richtig?
Solch ein dummer Fehler. Danke Freund :) – Podo
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