【发布时间】:2013-02-09 16:41:46
【问题描述】:
我试图输入一个复数并使用重载运算符来处理实部和虚部的解析。如果我输入像1 + 2i 这样的数字,我想要real = 1 和imaginary = 2。
现在如果我输入1 + 2i 输入,输出是1 + 0i。我怎样才能正确地做到这一点(号码有私人数据成员real和imaginary和operator>>是一个朋友功能)
//input the form of 1 + 2i
istream & operator>>(istream &in, Number &value)
{
in >> std::setw(1) >> value.real;
in.ignore(3); //skip 'space' and '+' and 'space'
in >> std::setw(1) >> value.imaginary;
in.ignore(); //skip 'i'
return in;
}
//output in the for 1 + 2i
ostream &
operator<<(ostream &out, const Complex &value)
{
out << value.real << " + " << value.imaginary << "i" <<std::endl;
return out;
}
【问题讨论】:
-
你试过使用调试器吗?
-
顺便说一句,this one 为我工作
-
该代码有什么错误?
标签: c++ operator-overloading cin