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 plaincopy
  1. size.Width = std::max(size.Width, elementSize.Width);  

3. 函數模板max   

  

[c-sharp] view plaincopy
  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 plaincopy
  1. size.Width = std::max(size.Width, elementSize.Width);  

                       修改爲:

                       

[c-sharp] view plaincopy
  1. size.Width = (std::max)(size.Width, elementSize.Width);  
發佈了33 篇原創文章 · 獲贊 40 · 訪問量 53萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章