[問題] 進出boost library的message_queue後 string變亂碼

看板C_and_CPP (C/C++)作者時間16年前 (2009/08/30 16:33), 編輯推噓0(000)
留言0則, 0人參與, 最新討論串1/1
Hi, 在使用boost library的message_queue時, 一模一樣的程式碼在兩個 不同的程式裡出現不一樣的結果. 下面的class是用來包裝資料並送進message_queue裡的. class msgPacket { public: msgPacket() : varInt(0), varBool(true), str("") {} ~msgPacket() {} void SetVarInt( int value ) { varInt = value; } void SetVarBool( bool value ) { varBool = value; } void SetStr( std::string value ) { str = value; } int GetVarInt() { return varInt; } bool GetVarBool() { return varBool; } std::string GetStr() { return str; } private: int varInt; bool varBool; std::string str; } 總共有三個程式 Sender A, Sender B, Receiver C 其中Receiver C裡面有一個loop, 會一直檢查message_queue裡面有沒有新進資料, 有的話就把它印出來. Sender A, Sender B裡都有以下幾行很簡單的送包裝資料後送進message_queue的動作. Sender A跟Sender B的差別在於, Sender A是一個很單純的測試用小程式, 而Sender B 是我們正在工作的專案中的一個程式. 以下幾行在Sender A, Sender B裡都有, 而且一模一樣. // 建立一個boost library的message_queue物件 message_queue* mq = new message_queue( open_or_create, "PluginMQ", 5000, sizeof( msgPacket ) ); // 建立資料包, 並將值設進去 msgPacket temp; temp.SetVarInt( 10 ); // 設varInt為10 temp.SetVarBool( true ); // 設varBool為true temp.SetStr( "tester" ); // 設str為tester // 送進message_queue mq->Send( &temp, sizeof( temp ), 0 ); 在編譯程式後, 資料包經由Sender A送進去message_queue, 由Receiver C接收後並顯示出來, 結果是正確的 10, true, tester 但是由Sender B送進去message_queue, Receiver C顯示出來的結果是 10, true, er後面幾個亂碼 能請問一下有人有遇過類似的情形嗎? 請問該怎麼解決呢? 謝謝 -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 113.10.92.233
文章代碼(AID): #1AcZbhI9 (C_and_CPP)
文章代碼(AID): #1AcZbhI9 (C_and_CPP)