看板 C_and_CPP 關於我們 聯絡資訊
開發平台(Platform): (Ex: Win10, Linux, ...) Win 10 編譯器(Ex: GCC, clang, VC++...)+目標環境(跟開發平台不同的話需列出) VC2017 額外使用到的函數庫(Library Used): (Ex: OpenGL, ...) Boost 問題(Question): Map底下以Key對應的Set無法有效分割 餵入的資料(Input): 兩人進Server,在同一個set 內進行foor迴圈廣播 預期的正確結果(Expected Output): 根據演算法,每個房間最多容納兩人, 以Map實現多房多人聊天 錯誤結果(Wrong Output): 房間切割不完整Map 程式碼(Code):(請善用置底文網頁, 記得排版,禁止使用圖檔) https://pastebin.com/N6dnFC7g 完整程式碼 typedef std::set<connection_hdl,std::owner_less<connection_hdl> > con_list; typedef std::map<int , con_list> HubToCon_list; // 房號,客戶列表 // HubToCon_list m_HubTo_list ; // 建立一個多通道(房) 聊天 int m_iKey = 0; //第0號房開始 int m_NumOfClient = 0; //房間人數 // //當有人連進進入 server if (a.type == SUBSCRIBE) { lock_guard<mutex> guard(m_connection_lock); //m_connections.insert(a.hdl);//原來範例使用set 更改為以下方式 if (m_NumOfClient > 2) { // 若房間大於2人 m_iKey = m_iKey + 1; // 開新房 m_NumOfClient = 0; // clean } m_HubTo_list[m_iKey].insert(a.hdl); //在Map<m_iKey,Set> 加入使用者 補充說明(Supplement): 參照Websocket開源 https://pastebin.com/y1ir8WbM 想做一個多人聊天室 卡在Map<int,set>無效加入其成員 上來詢問版友謝謝 -- ※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 60.248.167.253 ※ 文章網址: https://www.ptt.cc/bbs/C_and_CPP/M.1522467140.A.E58.html
steve1012: m_NumOfClient好像一直都是0? 03/31 11:42
steve1012: 寫個unit test 吧 可以避掉很多這種bug 03/31 11:43
djshen: 跟新房間有關的code就那幾行而已 自己試著debug一下吧 03/31 12:46
smartjay: 謝謝提醒,已經debug 03/31 18:12