C++ 错误:没有匹配的函数调用'min(long unsigned int&, unsigned int&)'

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14508022/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 18:23:43  来源:igfitidea点击:

error: no matching function for call to ‘min(long unsigned int&, unsigned int&)’

c++gccubuntuboost

提问by Will

I'm using ubuntu 12.04 - 64 bits. I tested it with boost 1.46, 1.48, 1.52 and gcc 4.4 and 4.6 When I try to compile:

我正在使用 ubuntu 12.04 - 64 位。我用 boost 1.46、1.48、1.52 和 gcc 4.4 和 4.6 对其进行了测试当我尝试编译时:

while (m_burstReqBeatsRemain) {
                if (m_burstReqAddress % m_dramRowSize == 0) {
                    m_admRequestQueue.push_back(adm_request());
                    adm_request &req = m_admRequestQueue.back();
                    req.address = m_burstReqAddress;
                    req.command = tlm::TLM_READ_COMMAND;
                    //call to min function
                    req.readLen = std::min(m_burstReqBeatsRemain * sizeof(Td), m_dramRowSize);
                }
                m_burstReqBeatsRemain--;
                m_burstReqAddress += sizeof(Td);
                m_ocpTxnQueue.push_back(m_ocpReq);
}

I get this error:

我收到此错误:

no matching function for call to ‘min(long unsigned int&, unsigned int&)
from /usr/include/c++/4.6/bits/stl_algobase.h*

Note: with ubuntu 12.04 32 bits works fine

注意:使用 ubuntu 12.04 32 位工作正常

Any idea how I can fix this?

知道我该如何解决这个问题吗?

回答by Nawaz

std::minis a function template on Twhich is the type of bothparameters of the function. But you seem to pass function arguments of different type, andrely on template argument deduction from function arguments, which is not possible.

std::min是一个函数模板,T其上是函数的两个参数的类型。但是您似乎传递了不同类型的函数参数,依赖于从函数参数中推导出模板参数,这是不可能的。

So the fix is :

所以修复是:

  • Either don't rely on template argument deduction, instead explicitly mention the template argument:

    std::min<unsigned long>(ulongarg, uintarg); //ok
         //^^^^^^^^^^^^^^^ 
         //don't rely on template argument deduction
         //instead pass template argument explicitly.
    
  • Or pass function arguments of same type:

    std::min(ulongarg, static_cast<unsigned long>(uintarg)); //ok
                      //^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
                      //pass both arguments of same type
    
  • 要么不依赖模板参数推导,而是明确提及模板参数:

    std::min<unsigned long>(ulongarg, uintarg); //ok
         //^^^^^^^^^^^^^^^ 
         //don't rely on template argument deduction
         //instead pass template argument explicitly.
    
  • 或者传递相同类型的函数参数:

    std::min(ulongarg, static_cast<unsigned long>(uintarg)); //ok
                      //^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
                      //pass both arguments of same type