"_comp"不能用作函数错误

'_comp' cannot be used as a function error

本文关键字:函数 错误 不能 comp      更新时间:2023-10-16

我正在获取"'_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))

这可能不是问题,但是您的min函数的标题指定它需要3个参数:类型__TP和类型_compare的2个参数,但在您的程序中,您可以使用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 !

编辑:如果您想找出三个数字的最小值,请考虑使用INT和float,请考虑指定Comp函数。要快速修复,请替换我在此中突出显示的行:

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