出现错误:控制到达非 void 函数的末尾不知道为什么
我不断收到以下错误,我不知道出了什么问题
cc1plus: warnings being treated as errors
scene.cpp: In member function ‘Image* Scene::getpicture(int) const’:
scene.cpp:179: error: control reaches end of non-void function
这是错误所在的代码部分:
Image* Scene::getpicture(int index) const {
if(index<0 || index >maximum)
cout << "invalid index" << endl;
else {
return images[index];
}
}
I keep receiving the following error and I have no idea whats wrong
cc1plus: warnings being treated as errors
scene.cpp: In member function ‘Image* Scene::getpicture(int) const’:
scene.cpp:179: error: control reaches end of non-void function
Here is the part of the code that the error is in:
Image* Scene::getpicture(int index) const {
if(index<0 || index >maximum)
cout << "invalid index" << endl;
else {
return images[index];
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果代码未输入
else
语句,则不会返回任何内容。因此,您需要在末尾或输入 if 语句时插入 return。If the code does not enter the
else
statement, nothing gets returned. Therefore you need to insert a return either at the end or when you enter the if-statement.当
if
语句的条件为 true 时,该函数不会返回值,因为在这种情况下没有执行return
语句。您需要返回一个值(或引发异常)。例如:
When the condition of the
if
statement is true, then the function doesn't return a value, because there is noreturn
statement that's executed in that case.You need to return a value (or throw an exception). For example: