【问题标题】:'_comp' cannot be used as a function error [duplicate]'_comp'不能用作函数错误[重复]
【发布时间】:2017-11-08 01:34:57
【问题描述】:

我在 stl_algobase.h 头文件中收到“'_comp' cannot be used as a function”错误。这是我的代码和应该有错误的头文件部分。 代码:

#include<iostream>
#include<algorithm>

using namespace std;

void subsqr(int a[10][10]){
    //int s[][10];
    for(int i =1;i<5;i++){
        for(int j = 1;j<5;j++){
            if(a[i][j] == 1){
                a[i][j] = min(a[i][j-1], a[i-1][j],a[i-1][j-1]) + 1;
            }
        }
    }
    for(int i=0;i<5;i++){
        for(int j=0;j<5;j++){
            cout<<a[i][j]<<"\t";
        }
        cout<<endl;
    }
}

int main(){
    int a[10][10] = {{0,1,1,0,1}, {1,1,0,1,0}, {1,1,1,0}, {1,1,1,1,0}, {1,1,1,1,1}, {0,0,0,0,0}};

    subsqr(a);  
    return 0;
}

stl_algobase.h:

 template<typename _Tp, typename _Compare>
    inline const _Tp&
    min(const _Tp& __a, const _Tp& __b, _Compare __comp)
    {
      //return __comp(__b, __a) ? __b : __a;
      if (__comp(__b, __a))
            return __b;
      return __a;
    }

编译器说错误在行

if (__comp(__b, __a))

【问题讨论】:

  • 不是双下划线前缀的名称可能被编译器保留吗?可能是冲突。
  • 你认为你调用的是哪个min()函数?
  • 请大家不要发布关于双下划线的无关 cmets。
  • 很确定 __comp 来自标准库。

标签: c++ multidimensional-array


【解决方案1】:

这可能不是问题,但您的 min 函数的标头指定它需要 3 个参数:2 个 __Tp 类型和 _Compare 类型之一,但在您的程序中,您使用 3 个 __TP 类型调用它:

a[i][j] = min(a[i][j-1], a[i-1][j],a[i-1][j-1]) + 1; // the third parameter is an int, not a function !

编辑:如果您想找出三个数字的最小值,请考虑 this post,使用 ints 和 float 不需要指定 comp 函数。 为了快速修复,请将我突出显示的行替换为:

std::min({a[i][j-1], a[i-1][j], a[i-1][j-1]});

【讨论】:

    猜你喜欢
    • 2018-07-23
    • 2019-06-22
    • 1970-01-01
    • 2018-01-09
    • 2016-05-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-07-19
    相关资源
    最近更新 更多