重载 << 运算符 c++ 的输出不正确
Incorrect output from overloaded << operator c++
我有Class NumDays
如图:
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);
}
我有 NumDay.cpp
其中包括:
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;
}
我还有 main.cpp
包括:
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
当我转到 运行 程序的这一小部分时,这是我的控制台:
有没有想过为什么 Sue 的时间错得离谱,而 John 的时间是正确的?
NumDays(double hr) { hr = hours; } //initializing constructor
哎呀。此处您保留成员 hours
未初始化并修改临时参数 hr
。你好像是说
NumDays(double hr) { hours = hr; }
(或更好:)
NumDays(double hr) : hours(hr) {}
NumDays(double hr) { hr = hours; }
应该是
NumDays(double hr) { hours = hr; }
问题是您打印了垃圾值,因为您的运算符 <<
调用了 NumDays
的默认值 copy constructor,它什么都不做。
NumDays(const NumDays& other){
}
这意味着您不打印之前已初始化的对象。
解法:
按如下方式更改您的运算符原型:
/* 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;
}
您的运算符 <<
原型只有在 `copy constructor' 像这样重载时才能工作:
NumDays(const NumDays& other){
hours = other.hours;
}
我觉得你不需要double
精度float
就够了
测试:g++ (Debian 4.9.2-10) 4.9.2
我有Class NumDays
如图:
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);
}
我有 NumDay.cpp
其中包括:
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;
}
我还有 main.cpp
包括:
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
当我转到 运行 程序的这一小部分时,这是我的控制台:
有没有想过为什么 Sue 的时间错得离谱,而 John 的时间是正确的?
NumDays(double hr) { hr = hours; } //initializing constructor
哎呀。此处您保留成员 hours
未初始化并修改临时参数 hr
。你好像是说
NumDays(double hr) { hours = hr; }
(或更好:)
NumDays(double hr) : hours(hr) {}
NumDays(double hr) { hr = hours; }
应该是
NumDays(double hr) { hours = hr; }
问题是您打印了垃圾值,因为您的运算符 <<
调用了 NumDays
的默认值 copy constructor,它什么都不做。
NumDays(const NumDays& other){
}
这意味着您不打印之前已初始化的对象。
解法:
按如下方式更改您的运算符原型:
/* 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;
}
您的运算符 <<
原型只有在 `copy constructor' 像这样重载时才能工作:
NumDays(const NumDays& other){
hours = other.hours;
}
我觉得你不需要double
精度float
就够了
测试:g++ (Debian 4.9.2-10) 4.9.2