今天 爲 使用 Ogre 開發的遊戲前端 添加網絡操做, 導入 網絡應用庫時出現了 類型衝突,前端
網絡應用庫定義了 UINT: // basetype.h ... typedef unsigned int UINT; // ExampleApplication.h ... using namespace Ogre; // net.h ... UINT port ; //沒法決策 // main.cpp #include "basetype.h" #include "ExampleApplication.h" #include "net.h"
編譯時 和 Ogre::UINT 衝突, 是編譯器 沒法決策使用哪一個 類型,開始的第一種解決方案是 修改 Ogre 的 ExampleApplication.h 等文件, 使不使用windows
using namespace Ogre;
但這樣就 修改了 Ogre; 第二種方式是修改 本來的 net 網絡應用庫, 使 自定義的的 UINT 在 命名空間 myLib 下:網絡
// basetype.h ... namespace myLib { typedef unsigned int UINT; }; 並在使用時(尤爲在 h 文件中), 用 full 方式使用: // net.h ... myLib::UINT port ;
不錯, 這樣 Ogre 編寫的這個遊戲前端可以有效編譯, 可是 在編譯另一個 只使用 網絡引用庫時, 編譯出錯, 出現函數
UINT : 不明確的符號 多是 D:\program files\microsoft sdks\windows\v7.0a\include\windef.h(173) : unsigned int UINT」 或 basetype.h : myLib::UINT
在一看 網絡應用庫的實現代碼中 通篇 都是 using namespace MyLib ; 語句, 是 MyLib 的 UINT 污染了 windef.h 的UINT 聲明, 就和上面的 Ogre::UINT 的 using namespace Ogre 衝突與 UINT, 爲此須要修改 網絡應用庫的 各個文件, 使都明確使用 myLib::UINT, 可是一旦某個文件中使用了spa
using namespace MyLib, 問題仍是存在, 因此 不適用 using namespace xxx 語句, 同時爲了書寫方便, 最終 網絡應用庫修改爲:code
聲明類型時使用 命名空間: namespace myLib { typedef unsigned int UINT; } // net.h 聲明時, 使用全名稱: myLib::UINT port; void func( myLib::UINT ID); // net.cpp 定義時, 在函數內部使用 using myLib::UINT: void func( myLib::UINT ID ) { using MyLib::UINT; UINT tempID = ID; }