-3

在堆中分配一个数组之后。我正在尝试创建一个函数 max 来查找带有指针的数组中的最大数字,但它给了我这个错误:-In function 'int main()': error:max,cannot用作函数。这是代码:

     #include<iostream>
     using namespace std;

    int max(int *v,int n){
       int i,max=0;
        for(i=1;i<=n;i++){
            if(*(v+i)>max)
                max=v[i];
        } 
        return max;
    }

     int main(){
     int *v,n,i;

  //read n    
     cout<<"Number of elements  ";
     cin>>n;

     v = new int[n];

    //read elements
    cout<<"Array Ellements:";
    for(i=1;i<=n;i++){
        cin>>v[i];
    }

    // output array elements   
    for(i=1;i<=n;i++){
        cout<<v[i];
        if(i<n)
        cout<<",";

    }
    cout<<endl;

     //max store the biggest number in the array
     int max;
     max = max(v,n);

        return 0;
    }
4

1 回答 1

0

您不能在同一范围内激活具有相同名称的函数和变量并同时使用它们。所以这永远行不通(正如你所发现的)

int max;
max = max(v,n); // the word 'max' now refers to the variable called 'max' not the function

int maxValue = max(v,n);
于 2018-10-02T20:35:46.650 回答