ostream 重载与 for 循环,无返回值
ostream overloading with for loop, no return value
我正在尝试重载我的 ostream 运算符<<,在函数的主体中我想使用 for 循环。内存是我做的一个类,它的内部结构是一个向量。所以基本上,我只想浏览矢量,并在将内存传递给输出流时打印出其中的所有内容。
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;
}
使用当前版本:
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;
}
}
如果curr == 0
,则不会返回任何内容。 您需要始终返回out
:
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; // outside the loop, so it always gets returned!
}
相关文章:
- 从python中调用C++函数并获取返回值
- 为什么模板类中的对象不能返回值
- 返回值优化:显式移动还是隐式
- lock_guard是否保护返回值
- 调用CreateProcess()并获取字符串的返回值
- 如何使 windows 命令提示符在C++可执行文件上显示返回值?
- 编译器警告:执行到达值返回函数的末尾而不返回值
- 查找 GCD:并非所有控制路径都返回值
- 返回值 mxGetPr() -- 等效循环
- 为循环中多次调用的函数返回值预分配内存的正确方法是什么
- C++ |如何循环 If 语句而不每次都返回值
- 内存分配,用于在C 11中循环中函数的返回值:如何优化
- 在返回值的函数中使用内部循环返回
- ostream 重载与 for 循环,无返回值
- 如果函数返回值,如何脱离循环
- C++ 如何处理循环中使用的 lambda 表达式for_each返回值
- 带有for循环的递归函数:返回值问题
- 如果我在main中有一个while(1)循环,我需要一个返回值吗
- 在循环中有常量函数返回值的优化吗?
- 使用返回值优化get和函数调用周围的循环