c++ - 在 C++ 中返回 Int 函数
问题描述
int r, i, arrayMinimumIndex(auto a)
{
for (int c : a)
c > a[r] ?: r = i, ++i;
return r;
}
我正在尝试运行此代码,但它显示:
[Error] a function-definition is not allowed here before '{' token
[Error] 'arrayMinimumIndex' was not declared in this scope
谁能解释它为什么会失败并修复它?提前致谢
解决方案
正确的函数定义如下所示:
int arrayMinimumIndex(auto a) //format: return type, methode name, parameters
{
int r = 0, i = 0; //variable definitions in the method body
// search the index..
return r;
}
或者
int r, i, arrayMinimumIndex(auto a);
也会起作用。r
在这种情况下i
是全局的。而且您仍然必须稍后实现该方法arrayMinimumIndex
(见上文)。
除此之外,如果您不使用 C++11(或更高版本),调用(int c: a)
将失败,因为简单数组没有实现迭代器。因此,您应该考虑通过例如一个std::vector
或手动遍历数组,例如for (int i = 0; i < ...; ++i)
推荐阅读
- javascript - 使用 IIFE 时未定义 jQuery
- xamarin - 是否是自定义渲染器的 DependencyService 替代品
- sql-server - 在 Visual Studio 2017 中安装 Oracle Attunity 驱动程序
- cypher - AgensGraph ver1.3 版本中使用的数组变量是什么?
- python - 操作数不能与形状一起广播 (5,15) (10,) (5,15)
- xmgrace - 如何在 xmgrace 中使用批处理脚本绘制简单图形
- java - 编写 Java Server 以同时(同时)处理多个客户端
- firebase - Flutter firestore listen 被上一页 firestore listen 打断
- r - R - 按组计算插值平均值的错误
- node.js - 如何从文档注释生成 Swagger UI?