3

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;
    }
halfer
  • 19,824
  • 17
  • 99
  • 186
  • C++ doesn't really have [variable-length arrays](https://en.wikipedia.org/wiki/Variable-length_array), though some compilers add it as a non-standard and non-portable extension. Use [`std::vector`](http://en.cppreference.com/w/cpp/container/vector) instead. – Some programmer dude Jun 29 '18 at 08:43
  • As for your error, please edit your question to include the *full* and *complete* copy-pasted (as text) error output, including any possible informational notes. Put it in the body of the question. And please show *where* in your code the error is, for example by adding a comment on that line. – Some programmer dude Jun 29 '18 at 08:44
  • 2
    Remove `using namespace std;`. – G.M. Jun 29 '18 at 08:46
  • 3
    And a small hint about the problem: [`std::max`](https://en.cppreference.com/w/cpp/algorithm/max) and [`std::min`](https://en.cppreference.com/w/cpp/algorithm/min)... And read [Why is “using namespace std” considered bad practice?](https://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice) – Some programmer dude Jun 29 '18 at 08:46
  • I am getting this error Compilation Error: Compilation Error prog.cpp: In function 'int main()': prog.cpp:31:18: error: invalid operands of types '' and 'int' to binary 'operator>' if(min > max) ^ prog.cpp:33:16: error: cannot resolve overloaded function 'min' based on conversion to type 'int' max=min; ^ – Priyam Rajvanshi Jun 29 '18 at 08:47

1 Answers1

5

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.

Bo Persson
  • 90,663
  • 31
  • 146
  • 203