将复杂的参数放入最低功能时会出现错误吗?为什么?(Eclipse C )

Getting error when putting complicated arguments in min function? Why?(Eclipse C++)

本文关键字:错误 为什么 Eclipse 参数 复杂 功能      更新时间:2023-10-16

我需要你的帮助

if(s[i]==t)
{
    //I get error for this
    //aSP[pos] = min( (dfs(i)+pow(i-pos,2)) , aSP[pos] );
    //Then I replace the above code with the following codes, and then it worked
    int a = (dfs(i)+pow(i-pos,2));
    int b = aSP[pos];            
    aSP[pos] = min(a,b);
}

但是它们是相同的对吗?为什么我会从Eclipse中遇到错误?
它说

描述资源路径位置类型 无效的论点 候选人是: const#0&min(const#0&,const#0&)

说明资源路径位置类型无匹配功能 到'min(__ __ GNU_CXX :: __ properte_2 :: __类型, int&)'tolledfulroad.h/corthfuload-c 线53 c/c 问题

以及其他一些信息,例如参数的冲突类型,模板参数扣除/替换失败。

错误消息意味着在此函数中调用

aSP[pos] = min( (dfs(i)+pow(i-pos,2)) , aSP[pos] );

第一个参数和第二个参数具有不同的类型。因此,编译器无法推论模板参数的类型。

您可以帮助编译器明确指定模板参数。例如

aSP[pos] = min<int>( (dfs(i)+pow(i-pos,2)) , aSP[pos] );

在函数的第二个呼叫中,两个参数都有type int。因此,将模板参数推导为int。

如果您有GCC错误,则更容易理解:

error: no matching function for call to 'min(double, int)'
     std::min(2.0, 3);
                    ^

只是将第一个参数施加到int。