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

2024-08-14C/C++开发问题
17

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

问题描述

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

Message 免费vpn is a class I made. vpn下载 vpn free I have a set of them in the main function that I pass to messageTimeOut (and some vpn下载 other functions). In messageTimeOut using an itorator I am looping vpn free 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 免费vpn下载 try to access non const member functions I get the error:

在函数‘void messageTimeOut(threadParameters*)’中:main.cpp:74:33: 错误:将const Message"作为this"参数传递'void vpn free 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 vpn下载 that I cannot access a non-const member vpn下载 function of a const Message vpn下载 object, but how do I go about making this a non const Message object so I can access non const member vpn free vpn下载 functions and change 免费vpn the Message? Thanks

我的部分代码:

  free vpn  vpn下载   [ . . . ]

void messageTimeOut( threadParameters* params 免费vpn下载 )
{
     set<Message>::iterator it = params->messages->begin();
  vpn free   免费vpn  [ . . . ]
    for ( ; it != 免费vpn下载 params->messages->end(); ++it )
    {
        if ( (it->createdTime() + RESPONSE_WAIT) < GetTickCount() ) 
     vpn下载    {
    vpn free         it->setTimedOut(true); // 免费vpn error 
  免费vpn  vpn 免费vpn下载 free   免费vpn    }
    }
    免费vpn ReleaseMutex(sentQueueMutex);
}

   free vpn free vpn   [ . . . ]

int main()
{
    threadParameters rmparameters;
    set<Message> 免费vpn sentMessages;
  免费vpn 免费vpn下载    [ free vpn vpn下载 . . . ]


    rmparameters.logFile = &logFile;
    rmparameters.socket = socketDesciptor;
    rmparameters.messages = &sentMessages;
 vpn下载      [ . . . ]

    messageTimeOut( rmparameters );
      [ . . . ]

   free vpn  return vpn下载 免费vpn下载 0;
}

推荐答案

你不能.

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

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

请注意,您调用的函数不会更改顺序并不重要,因为 vpn free 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 vpn free to use a vpn free map, even if this would probably force you to duplicate some data.

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

The End

相关推荐

无法访问 C++ std::set 中对象的非常量成员函数
Unable to access non-const member functions of objects in C++ std::set(无法访问 C++ std::set 中对象的非常量成员函数)...
2024-08-14 C/C++开发问题
17

从 lambda 构造 std::function 参数
Constructing std::function argument from lambda(从 免费vpn lambda 构造 std::function 参数)...
2024-08-14 C/C++开发问题
25

STL BigInt 类实现
STL BigInt class implementation(STL BigInt 类实现)...
2024-08-14 C/C++开发问题
3

使用 std::atomic 和 std::condition_variable 同步不可靠
Sync is unreliable using std::atomic and std::condition_variable(使用 std::atomic 和 std::condition_variable 同步不可靠)...
2024-08-14 C/C++开发问题
17

在 STL 中将列表元素移动到末尾
Move list element to the end in STL(在 STL 中将列表元素移动到末尾)...
2024-08-14 C/C++开发问题
9

为什么禁止对存储在 STL 容器中的类重载 operator&amp;()?
Why is overloading operatoramp;() prohibited for classes 免费vpn stored in vpn下载 free vpn STL containers?(为什么禁止对存储在 STL 容器中的类重载 operatoramp;()?)...
2024-08-14 C/C++开发问题
6