如果返回类型是对象,是否可能削减编译器投诉?

0 c++

 pseudocode :
  function takes "int c" and "list d" 
          from start to end of the list search "c" in "list d"
               when see it 
                    return index
Run Code Online (Sandbox Code Playgroud)

由于迭代中写入返回,编译器开始抱怨非void函数.如何减少编译器投诉?

注意:c始终在列表中,但不知道它的索引.

编辑:如果我改变如下;

  pseudocode :
  function takes "string name" and "list d" 
          from start to end of the list search "c" in "list d"
               when see it 
                    return object
Run Code Online (Sandbox Code Playgroud)

我该怎么办 ?

Mat*_*Mat 5

发布真实代码确实会有所帮助.我猜你有类似的东西:

int find(int c, list d) {
  for (*iterate over list*) {
    if (item == c)
      return index;
  }
  // <- nothing here
}
Run Code Online (Sandbox Code Playgroud)

在大多数情况下,编译器无法知道列表将始终包含c.所以你需要添加一个return语句.这样的事情很平常:

int find(int c, list d) {
  for (*iterate over list*) {
    if (item == c)
      return index;
  }
  // Never reached
  return -1; // or throw an exception
}
Run Code Online (Sandbox Code Playgroud)

(或选择另一个无效的索引值).请留下详细评论,说明为什么永远不会达到该部分.如果您已经使用它们,那么抛出异常可能是一个好主意 - 如果/当您对列表的假设始终包含c失败时,它会发现错误.