' _comp'不能用作函数错误

时间:2017-06-06 21:26:45

标签: c++ multidimensional-array

我正在"' _comp'不能用作功能" stl_algobase.h头文件中的错误。这是我的代码和应该有错误的头文件的一部分。 代码:

#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))

1 个答案:

答案 0 :(得分:4)

这可能不是问题,但你的min函数的标题指定它需要3个参数:类型__Tp的2个和_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,使用整数和浮点数来指定一个comp函数。 要快速修复,请将我突出显示的行替换为:

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