当我尝试编译程序时,我遇到了一堆错误,例如:'std :: max':找不到匹配的重载函数

Sil*_*rut 2 c++ compiler-errors

当我尝试编译程序时,我得到了一堆错误,如:

'std::max': no matching overloaded function found

 'const _Ty &std::max(const _Ty &,const _Ty &,_Pr) noexcept(<expr>)': expects 3 arguments - 2 provided


 C2782: 'const _Ty &std::max(const _Ty &,const _Ty &) noexcept(<expr>)': template parameter '_Ty' is ambiguous


  C2784: 'const _Ty &std::max(const _Ty &,const _Ty &) noexcept(<expr>)': could not deduce template argument for 'const _Ty &' from 'int'
Run Code Online (Sandbox Code Playgroud)

以下是导致大多数问题的代码:

 template <typename TVar>
    void CopyVar( void*& pTarget, const void*& pSource, int nAlign = 4 )
    {
        *((TVar*) pTarget) = *((TVar*) pSource);
        ((BYTE*&) pTarget) += max( sizeof(TVar), nAlign );
        ((BYTE*&) pSource) += max( sizeof(TVar), nAlign );
    }
Run Code Online (Sandbox Code Playgroud)

有人可以帮我弄这个吗.

Som*_*ude 6

sizeof运算符返回类型的值size_t,它是一个无符号的整数类型.

您调用std::max混合两种不同类型(无符号size_t和有符号int),并std::max要求两个参数属于同一类型.

正如评论中所提到的,合适的解决方案是使nAlign变量具有相同的类型,即a size_t.对齐永远不会是负面的,也是一种尺寸.它还确保类型大小相同(size_t可以是64位类型,unsigned int通常是32位).

如果nAlign无法更改类型,那么你应该将结果转换sizeofint(它更安全,因为有人可以传递负值,nAlign如果转换为无符号类型会产生不良后果).

  • 或者显式调用`std::max&lt;std::size_t&gt;`。 (2认同)