小编wbr*_*wbr的帖子

在eclipse c ++中"控制到达非void函数的结束"警告但没有编译或运行时错误

这是我的代码:

Composer& Database::GetComposer (string in_last_name)
{   
    for (int i = 0; i < next_slot_; i++)
    {
        if (composers_[i].last_name() == in_last_name)
             return composers_[i];
    }
}
Run Code Online (Sandbox Code Playgroud)

我们的想法是遍历一个Composer对象数组,并返回对last_name字段与"in_last_name"匹配的对象的引用.我理解警告告诉我的是,该函数可能不会返回任何内容(例如,如果用户提供了无效的姓氏).我的问题是,我该如何避免这种情况?我尝试在for循环后添加"return 0"和"return NULL",它不会编译.如果找不到任何内容,此方法是否应抛出异常?

c++ eclipse null return

5
推荐指数
1
解决办法
975
查看次数

标签 统计

c++ ×1

eclipse ×1

null ×1

return ×1