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)
]一样手动遍历数组