c++ - 错误 : invalid operands of types '<unresolved overloaded function type>' and 'int' to binary 'operator>' if(min > max)

标签 c++

我在编译这个程序时遇到这个错误。这个问题是找到最小窗口大小的最大值。这是我的代码:

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

最佳答案

您正在通过 int max整数分钟。然而,这种阴影只发生在本地范围内,也就是声明这些变量的地方。

当您离开作用域时,外部名称将再次可见:

        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

因此编译器认为min > max是将一个int与一个函数模板进行比较。它看不到如何做到这一点。

这就是为什么使用 using namespace std; 被认为不是一个好主意的原因之一。

关于c++ - 错误 : invalid operands of types '<unresolved overloaded function type>' and 'int' to binary 'operator>' if(min > max),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51097649/

相关文章:

c++ - 使用相同的临界区对象读写

c++ - volatile 成员的访问方法

xerces - Xerces-C 中的 XPath 支持

c++ - 用常量值填充 std::vector<double>

c++ - 类指针的动态数组

c++ - Clang 和 GCC 在强制转换 C++17 中的非类型模板参数的自动说明符中存在分歧

c++ - 将属性树 boost 为字符串

c++ - 从标准库类型特征继承

c++ - Clang Format多行格式配置错误

c++ - c++ 是否为引用指定了哈希函数?