为什么我不能在 C++ 中调用 max 函数?

Why I can't call max function in C++?

在 heap.I 中分配一个数组后,我试图创建一个函数 max 来查找带有指针的数组中的最大数字,但它给了我这个错误:-In function 'int main() ': error:max,不能作为函数使用。 这是代码:

     #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;
    }

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

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

int maxValue = max(v,n);