【问题标题】:C++ Input Overload ErrorC++ 输入重载错误
【发布时间】:2016-04-16 17:09:56
【问题描述】:
istream& operator>>(istream& input, const complex& P) {
        input >> P.real >> P.imaginary;
        return input;
}

我有上面的代码,但由于某种原因,我收到了这个错误:

Invalid operands to binary expression ('istream' (aka 'basic_istream<char>') and 'double').

complex& P 是一个对象,它基本上存储复数的实数部分和虚数部分。所以如果你有 2 + 3i,P.real 会返回给你 2,P.imaginary 会返回给你 3。实数和虚数都是双倍的。

谁能帮我解决这个问题?

【问题讨论】:

  • 从 const complex 中移除 const。

标签: c++


【解决方案1】:

当重载>>运算符时,你要分配输入值的变量必须是可变的,所以你需要去掉“const”

istream& operator>>(istream& input, complex& P) {
  input >> P.real >> P.imaginary;
  return input;
}

【讨论】:

  • 是啊刚刚意识到哈哈...小错误...感谢您的帮助!
猜你喜欢
  • 1970-01-01
  • 2011-10-11
  • 1970-01-01
  • 2013-05-26
  • 1970-01-01
  • 2022-09-29
  • 2016-05-07
  • 1970-01-01
  • 2018-09-14
相关资源
最近更新 更多