首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >“operator<<”必须返回值吗?

“operator<<”必须返回值吗?
EN

Stack Overflow用户
提问于 2021-05-19 23:21:36
回答 2查看 13关注 0票数 0

从这段代码中获取Visual Studio 2019中的编译器错误c4716 ('operator<<‘必须返回值):

代码语言:javascript
运行
复制
// Friend function to print the towers to stream
ostream& operator<<(ostream& stream, const TheGame& game) {
stream
    << "Tower #0:" << game.towers[0] << endl
    << "Tower #1:" << game.towers[1] << endl
    << "Tower #2:" << game.towers[2] << endl
    ;
}

如有任何帮助,将不胜感激,谢谢!

EN

回答 2

Stack Overflow用户

发布于 2021-05-19 23:25:44

您的代码中没有任何值的return。如错误所示,您的operator<<必须返回一个值(在本例中可能是原始流,以便可以链接更多的流操作,即return stream;)。

例如,2 << 3将返回16stream << something通常返回stream,以便您可以在其返回值的末尾添加更多<<操作。由于您正在实现自己的operator<<,因此也需要注意这一点。

票数 1
EN

Stack Overflow用户

发布于 2021-05-20 01:42:06

代码语言:javascript
运行
复制
// Friend function to print the towers to stream
ostream& operator<<(ostream& stream, const TheGame& game) {
    return stream
        << "Tower #0:" << game.towers[0] << endl
        << "Tower #1:" << game.towers[1] << endl
        << "Tower #2:" << game.towers[2] << endl
        ;
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/67606144

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档