简体   繁体   中英

error: invalid operands of types '<unresolved overloaded function type>' and 'int' to binary 'operator>' if(min > max)

I am getting this error while compiling this program. This questions is to find max of min's window size. Here is my code:

    #include <iostream>

    using namespace std;

    int main() {

            int n;
            cin>>n;
            int a[n];
            for(int i=0; i<n; i++)
            {
                cin>>a[i];//to take input as array
            }
            for(int k=1; k<=n; k++)
            {
                int max=0;
                for(int i=0; i<n-k; i++)
                {
                    int min=a[i];
                    for(int j=0; j<k; j++)
                    {
                        if(a[i+j]<min)
                        {
                            min=a[i+j];
                        }
                    }
                }
               if(min > max)
               {
                   max=min;
               }
                cout<<max<<" ";
            }

        return 0;
    }

You are hiding (shadowing) the std::min and std::max functions by the local declarations of int max and int min . However, this shadowing only happens in the local scope, where these variables are declared.

When you leave the scope, the outer name becomes visible again:

        int max=0;                  // <-- Here max hides std::max
        for(int i=0; i<n-k; i++)
        {
            int min=a[i];          // <-- Here min hides std::min
            for(int j=0; j<k; j++)
            {
                if(a[i+j]<min)
                {
                    min=a[i+j];
                }
            }
        }                       // <-- But here the local min goes away again

       if(min > max)            // <-- And here min is std::min, a function

So the compiler believes that min > max is to compare an int to a function template. And it cannot see how to do that.

This is one of the reasons why using using namespace std; is considered not a good idea.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM