【问题标题】:Getting error when putting complicated arguments in min function? Why?(Eclipse C++)将复杂参数放入 min 函数时出错?为什么?(Eclipse C++)
【发布时间】:2017-07-08 18:02:33
【问题描述】:

我需要你的帮助

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::__promote_2::__type, int&)' ColorfulRoad.h /colorfulroad-c++ line 53 C/C++ 问题

还有一些其他信息,比如参数类型冲突、模板参数推导/替换失败..

【问题讨论】:

    标签: c++ eclipse templates arguments min


    【解决方案1】:

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

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

    只需将第一个参数转换为 int。

    【讨论】:

      【解决方案2】:

      错误信息表示在这个函数调用中

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

      第一个和第二个参数有不同的类型。所以编译器无法推断出模板参数的类型。

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

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

      在函数的第二次调用中,两个参数的类型都是 int。所以模板参数推导出为int。

      【讨论】:

        猜你喜欢
        • 2021-01-12
        • 1970-01-01
        • 2019-10-28
        • 1970-01-01
        • 2020-02-05
        • 2011-04-14
        • 1970-01-01
        • 2018-08-19
        • 2020-01-02
        相关资源
        最近更新 更多