【发布时间】:2021-09-09 23:18:04
【问题描述】:
我正在尝试编写 << 运算符。我的代码:
#include <iostream>
#include <ostream>
class sound_ctl {
private:
int _vol;
bool _status;
public:
sound_ctl() : _vol(50), _status(false) {};
void operator++() {
if (_vol <= 98) {
_vol += 2;
}
}
void operator--() {
if (_vol >= 2) {
_vol -= 2;
}
}
void operator!() {
_status = !_status;
}
std::ostream &operator<<(std::ostream &os) const {
os << _status << "Volume: " + _vol << std::endl;
return os;
}
};
int main() {
sound_ctl panel1;
std::cout << panel1;
}
但是,代码不会编译错误:Invalid operands to binary expression ('std::ostream' (aka 'basic_ostream<char>') and 'sound_ctl')
我严重缺乏对 C++ 中的流的理解,因此,了解潜在问题会对我有很大帮助。谢谢
【问题讨论】: