我是 C++ 的新手,在使用 max
函数时遇到了这个意外错误,我在其中传递了 long long 和 int 类型的参数
当我将 (int,int)
作为参数或 (long long,long long)
传递时,它工作正常但与 (long long,int) 不兼容
ll painter(int board[],int n,int k)
{
ll s = 0,total_min;
ll ans;
for(ll i = 0;i < n;i++)
{ total_min += board[i];
s = max(s,board[i]);
}
这是错误显示
prog.cpp:39:25: error: no matching function for call to 'max(int&, long long int&)
s =max(s,board[i]);"
最佳答案
因为 std::max
是一个函数模板,其签名例如是
template< class T >
const T& max( const T& a, const T& b );
完整列表位于:https://en.cppreference.com/w/cpp/algorithm/max
现在,在实例化模板时,您提供了两种不同的类型,但模板只需要一种。您必须明确指定模板参数,如本例所示:
#include <algorithm>
int main(){
int a{5};
long long int b{55};
return std::max<long long int>(a, b);
}
如评论中所述(用户 John),您还可以转换两者之一,例如,您可以将 int
转换为 long long
关于c++ - 当我使用 long long int 和 int 作为参数时,为什么 max 函数会出现错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57231493/