我正在尝试重载我的 ostream 运算符<<,并且在函数体中我想使用 for 循环。 Memory是我做的一个类,它的内部结构是一个vector。所以基本上,当我将内存传递给输出流时,我只想遍历 vector 并打印出其中的所有内容。
std::ostream& operator<<(std::ostream& out, const Memory& mem)
{
int curr(mem.get_current());
for (int i = 0; i <= curr; ++i)
{
return out << mem.mem_[i] << std::endl;
}
}
编译器说在返回非 void 的函数中没有返回值。
最佳答案
std::ostream& operator<<(std::ostream& out, const Memory& mem) {
int curr(mem.get_current());
for (int i = 0; i <= curr; ++i) {
out << mem.mem_[i] << std::endl;
}
return out;
}
关于c++ - ostream 重载 for 循环,没有返回值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19637104/