<bdo id='ul1TG'></bdo><ul id='ul1TG'></ul>
  • <small id='ul1TG'></small><noframes id='ul1TG'>

  • <i id='ul1TG'><tr id='ul1TG'><dt id='ul1TG'><q id='ul1TG'><span id='ul1TG'><b id='ul1TG'><form id='ul1TG'><ins id='ul1TG'></ins><ul id='ul1TG'></ul><sub id='ul1TG'></sub></form><legend id='ul1TG'></legend><bdo id='ul1TG'><pre id='ul1TG'><center id='ul1TG'></center></pre></bdo></b><th id='ul1TG'></th></span></q></dt></tr></i><div id='ul1TG'><tfoot id='ul1TG'></tfoot><dl id='ul1TG'><fieldset id='ul1TG'></fieldset></dl></div>

    <tfoot id='ul1TG'></tfoot>

      <legend id='ul1TG'><style id='ul1TG'><dir id='ul1TG'><q id='ul1TG'></q></dir></style></legend>

        无法访问 C++ std::set 中对象的非常量成员函数

        时间:2024-08-14

              <tbody id='ErKIL'></tbody>
            <tfoot id='ErKIL'></tfoot>
              • <bdo id='ErKIL'></bdo><ul id='ErKIL'></ul>
              • <i id='ErKIL'><tr id='ErKIL'><dt id='ErKIL'><q id='ErKIL'><span id='ErKIL'><b id='ErKIL'><form id='ErKIL'><ins id='ErKIL'></ins><ul id='ErKIL'></ul><sub id='ErKIL'></sub></form><legend id='ErKIL'></legend><bdo id='ErKIL'><pre id='ErKIL'><center id='ErKIL'></center></pre></bdo></b><th id='ErKIL'></th></span></q></dt></tr></i><div id='ErKIL'><tfoot id='ErKIL'></tfoot><dl id='ErKIL'><fieldset id='ErKIL'></fieldset></dl></div>
                <legend id='ErKIL'><style id='ErKIL'><dir id='ErKIL'><q id='ErKIL'></q></dir></style></legend>

                  <small id='ErKIL'></small><noframes id='ErKIL'>

                  本文介绍了无法访问 C++ std::set 中对象的非常量成员函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  Message 是我做的一个类.我在传递给 messageTimeOut(和其他一些函数)的主函数中有一组它们.在使用迭代器的 messageTimeOut 中,我循环遍历它们并访问不同的成员函数.但是,我只能访问迭代器指向的 Message 的 const 成员函数.如果我尝试访问非 const 成员函数,则会出现错误:

                  Message is a class I made. I have a set of them in the main function that I pass to messageTimeOut (and some other functions). In messageTimeOut using an itorator I am looping through them and accessing different member functions. However, I can only access const member functions of the Message pointed to by the iterator. If I try to access non const member functions I get the error:

                  在函数‘void messageTimeOut(threadParameters*)’中:main.cpp:74:33: 错误:将const Message"作为this"参数传递'void Message::setTimedOut(bool)' 丢弃限定符 [-fpermissive]."

                  "In function 'void messageTimeOut(threadParameters*)': main.cpp:74:33: error: passing 'const Message' as 'this' argument of 'void Message::setTimedOut(bool)' discards qualifiers [-fpermissive]."

                  我无法访问 const Message 对象的非常量成员函数是有道理的,但是我如何使它成为非常量 Message 对象,以便我可以访问非常量成员函数并更改消息?谢谢

                  It makes sense that I cannot access a non-const member function of a const Message object, but how do I go about making this a non const Message object so I can access non const member functions and change the Message? Thanks

                  我的部分代码:

                       [ . . . ]
                  
                  void messageTimeOut( threadParameters* params )
                  {
                       set<Message>::iterator it = params->messages->begin();
                       [ . . . ]
                      for ( ; it != params->messages->end(); ++it )
                      {
                          if ( (it->createdTime() + RESPONSE_WAIT) < GetTickCount() ) 
                          {
                              it->setTimedOut(true); // error 
                          }
                      }
                      ReleaseMutex(sentQueueMutex);
                  }
                  
                       [ . . . ]
                  
                  int main()
                  {
                      threadParameters rmparameters;
                      set<Message> sentMessages;
                       [ . . . ]
                  
                  
                      rmparameters.logFile = &logFile;
                      rmparameters.socket = socketDesciptor;
                      rmparameters.messages = &sentMessages;
                        [ . . . ]
                  
                      messageTimeOut( rmparameters );
                        [ . . . ]
                  
                      return 0;
                  }
                  

                  推荐答案

                  你不能.

                  std::set 中的元素总是常量,否则用户可以通过修改属于集合的元素来修改集合中的排序.

                  Elements inside an std::set are always constant, because otherwise an user could modifiy the ordering in the set by modifying an element which belong to the set.

                  请注意,您调用的函数不会更改顺序并不重要,因为 std::set 无法检查这一点.

                  Notice that it doesn't matter that the function that you invoke doesn't change the ordering, since std::set has no way to check this.

                  解决这个问题的常用方法是从集合中删除元素,修改它,然后重新插入它.另一种方法是使用地图,即使这可能会迫使您复制一些数据.

                  A common way to fix this is to remove the element from the set, modify it, then reinsert it. Another way would be to use a map, even if this would probably force you to duplicate some data.

                  这篇关于无法访问 C++ std::set 中对象的非常量成员函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:应该如何实现大小受限的 stl 类容器? 下一篇:没有了

                  相关文章

                  <tfoot id='f8aag'></tfoot><legend id='f8aag'><style id='f8aag'><dir id='f8aag'><q id='f8aag'></q></dir></style></legend>
                1. <small id='f8aag'></small><noframes id='f8aag'>

                  <i id='f8aag'><tr id='f8aag'><dt id='f8aag'><q id='f8aag'><span id='f8aag'><b id='f8aag'><form id='f8aag'><ins id='f8aag'></ins><ul id='f8aag'></ul><sub id='f8aag'></sub></form><legend id='f8aag'></legend><bdo id='f8aag'><pre id='f8aag'><center id='f8aag'></center></pre></bdo></b><th id='f8aag'></th></span></q></dt></tr></i><div id='f8aag'><tfoot id='f8aag'></tfoot><dl id='f8aag'><fieldset id='f8aag'></fieldset></dl></div>

                      • <bdo id='f8aag'></bdo><ul id='f8aag'></ul>