error C2589: “(”: “::”右边的非法标记;error C2059: 语法错误 : “::”

1. 错误输出

    ./zlibrary/ui/src/win32/w32widgets/W32VBorderBox.cpp(114) : error C2589: “(”: “::”右边的非法标记
    ./zlibrary/ui/src/win32/w32widgets/W32VBorderBox.cpp(114) : error C2059: 语法错误 : “::”
2. 错误代码举例

  

[c-sharp]  view plain copy
  1. size.Width = std::max(size.Width, elementSize.Width);  

3. 函数模板max   

  

[c-sharp]  view plain copy
  1. template<class _Ty> inline  
  2.     const _Ty& (__CLRCALL_OR_CDECL max)(const _Ty& _Left, const _Ty& _Right)  
  3.     {   // return larger of _Left and _Right  
  4.     return (_DEBUG_LT(_Left, _Right) ? _Right : _Left);  
  5.     }  
   

   注:模板就是实现代码重用机制的一种工具,它可以实现类型参数化,即把类型定义为参数, 从而实现了真正的代码可重用性。模版可以分为两类,一个是函数模版,另外一个是类模版。

4. 错误原因

   函数模板max与Visual C++中的全局的宏max冲突。 

5. 解决办法

    第一种办法:设置项目属性,在预定义处理器中添加定义NOMINMAX来禁止使用Visual C++的min/max宏定义。

                       项目属性   ——> C/C++ ——> 预处理器 ——> 预处理器定义 (此处添加预定义编译开关   NOMINMAX)

但是visual C++中定义能自动匹配double和int,如果进行了上述设置,代码中手动将int型的数据乘以1.0来达到double的目的。

    第二种办法: 加上括号,与Vsual C++的min/max宏定义区分开

                      

[c-sharp]  view plain copy
  1. size.Width = std::max(size.Width, elementSize.Width);  

                       修改为:

                       

[c-sharp]  view plain copy
  1. size.Width = (std::max)(size.Width, elementSize.Width); 

你可能感兴趣的:(error C2589: “(”: “::”右边的非法标记;error C2059: 语法错误 : “::”)