将复杂参数放入 min 函数时出错?为什么?(Eclipse C++)
Getting error when putting complicated arguments in min function? Why?(Eclipse C++)
我需要你的帮助
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 中收到错误消息?
它说
Description Resource Path Location Type
Invalid arguments '
Candidates are:
const #0 & min(const #0 &, const #0 &)
Description Resource Path Location Type no matching function for call
to 'min(__gnu_cxx::__promote_2::__type,
int&)' ColorfulRoad.h /colorfulroad-c++ line 53 C/C++ Problem
以及一些其他信息,例如参数类型冲突、模板参数 deduction/substitution 失败..
如果你有GCC错误反而更容易理解:
error: no matching function for call to 'min(double, int)'
std::min(2.0, 3);
^
只需将第一个参数转换为 int。
错误信息的意思是在这个函数调用
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.
我需要你的帮助
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 中收到错误消息?
它说
Description Resource Path Location Type Invalid arguments ' Candidates are: const #0 & min(const #0 &, const #0 &)
Description Resource Path Location Type no matching function for call to 'min(__gnu_cxx::__promote_2::__type, int&)' ColorfulRoad.h /colorfulroad-c++ line 53 C/C++ Problem
以及一些其他信息,例如参数类型冲突、模板参数 deduction/substitution 失败..
如果你有GCC错误反而更容易理解:
error: no matching function for call to 'min(double, int)'
std::min(2.0, 3);
^
只需将第一个参数转换为 int。
错误信息的意思是在这个函数调用
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.