在C ++中返回Int函数

问题描述 投票:-1回答:1
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

谁能解释为什么它会失败并解决?在此先感谢

c++ int return
1个回答
1
投票

正确的函数定义如下所示:

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);

也将起作用。在这种情况下,ri是全局的。而且仍然需要稍后实现方法arrayMinimumIndex(请参见上文)。

此外,如果不使用C ++ 11(或更高版本),则调用(int c: a)将失败,因为简单数组未实现迭代器。因此,您应该考虑通过std::vector或像for (int i = 0; i < ...; ++i)]一样手动遍历数组

© www.soinside.com 2019 - 2024. All rights reserved.