求排错,用了boost::mutex之后就编译不过

  1. #include <iostream>
  2. #include <deque>
  3. using namespace std;
  4. #include <boost/thread/thread.hpp>
  5. // 基本消息对象
  6. typedef struct
  7. {
  8. int messageId;        // 若Id为0则为无效消息
  9. }RobotMessage;

  10. // 消息对列对象
  11. class RobotMessageQueue
  12. {                             // 编译错误说的第17行
  13.     private:
  14. // 消息对了未定义
  15. boost::mutex        messageQueueMutex; // 用来保护消息对列的读写,就是加上这句总是编译不过
  16.     public:
  17. void AppendMessage(RobotMessage message);  // 消息入队
  18. void ProvideMessage(RobotMessage * message);// 消息出队
  19. };

  20. void RobotMessageQueue::AppendMessage(RobotMessage message)
  21. {

  22. //boost::mutex::scoped_lock lock(this->messageQueueMutex);
  23. return;
  24. }

  25. void RobotMessageQueue::ProvideMessage(RobotMessage * message)
  26. {
  27. //boost::mutex::scoped_lock lock(this->messageQueueMutex);
  28. // ....
  29. return;
  30. }

  31. class RobWen  // 机器人对象
  32. {
  33.     private:
  34. RobotMessageQueue speakMessageQueue;
  35. boost::thread    *sensingThread;     // 一个感知线程
  36. boost::thread     *speakingThread;  // 一个语音线程

  37.     public:
  38. RobWen();
  39. ~RobWen();
  40. void Read();  // 感知线程函数,发送消息入队
  41. void Speak(); // 语音线程函数,提取消息出队

  42. };


  43. RobWen::RobWen()
  44. {
  45. sensingThread = new boost::thread(boost::bind(&RobWen::Read, this));

  46. speakMessageQueue = RobotMessageQueue();  // 这一句也总是报错,编译错误说的第57行
  47. speakingThread = new boost::thread(boost::bind(&RobWen::Speak, this));
  48. }

  49. RobWen::~RobWen()
  50. {
  51. delete    sensingThread;
  52. delete    speakingThread;
  53. }

  54. void RobWen::Read()
  55. {
  56. while(true)
  57.     {
  58.    
  59.         RobotMessage message;
  60.         message.messageId = 1;
  61.         speakMessageQueue.AppendMessage(message);
  62.     }

  63. return;
  64. }


  65. void RobWen::Speak()
  66. {

  67. while(true)
  68.      {
  69.     // handle the message
  70.     RobotMessage message;
  71.     this->speakMessageQueue.ProvideMessage(&message);
  72.     if(message.messageId != 0)
  73.         {
  74.         cout << "收到消息,Id:" << message.messageId << endl;   
  75.         }
  76.      }

  77. }
复制代码
把编译和错误代码也贴上来吧~~
  1. g++ -c -o errorTest RobWen2_edit.cc
  2. In file included from /usr/include/boost/thread/mutex.hpp:16,
  3.                  from /usr/include/boost/thread/pthread/thread_data.hpp:12,
  4.                  from /usr/include/boost/thread/thread.hpp:17,
  5.                  from RobWen2_edit.cc:8:
  6. /usr/include/boost/noncopyable.hpp: In member function ‘boost::mutex& boost::mutex::operator=(const boost::mutex&)’:
  7. /usr/include/boost/noncopyable.hpp:28: error: ‘const boost::noncopyable_::noncopyable& boost::noncopyable_::noncopyable::operator=(const boost::noncopyable_::noncopyable&)’ is private
  8. /usr/include/boost/thread/pthread/mutex.hpp:31: error: within this context
  9. RobWen2_edit.cc: In member function ‘RobotMessageQueue& RobotMessageQueue::operator=(const RobotMessageQueue&)’:
  10. RobWen2_edit.cc:17: note: synthesized method ‘boost::mutex& boost::mutex::operator=(const boost::mutex&)’ first required here
  11. RobWen2_edit.cc: In constructor ‘RobWen::RobWen()’:
  12. RobWen2_edit.cc:57: note: synthesized method ‘RobotMessageQueue& RobotMessageQueue::operator=(const RobotMessageQueue&)’ first required here   
复制代码

作者: trypsin   发布时间: 2011-06-13

g++ -c -o errorTest RobWen2_edit.cc -pthread

作者: int-main   发布时间: 2011-06-13