从这段代码中获取Visual Studio 2019中的编译器错误c4716 ('operator<<‘必须返回值):
// 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
;
}如有任何帮助,将不胜感激,谢谢!
发布于 2021-05-19 23:25:44
您的代码中没有任何值的return。如错误所示,您的operator<<必须返回一个值(在本例中可能是原始流,以便可以链接更多的流操作,即return stream;)。
例如,2 << 3将返回16。stream << something通常返回stream,以便您可以在其返回值的末尾添加更多<<操作。由于您正在实现自己的operator<<,因此也需要注意这一点。
发布于 2021-05-20 01:42:06
// 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
;
}https://stackoverflow.com/questions/67606144
复制相似问题