本文介绍了不是一个迭代器在c ++一种指针?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 Ok这次我决定使用STL列表。我需要为每个客户端创建一个专用的TCP套接字。所以每次我有一个连接,我实例化一个套接字并添加一个指针在列表上。 list< MyTcp *> SocketList; //这是指向套接字的指针列表 list< MyTcp *> :: iterator it; //一个迭代器到指向TCP套接字的指针列表。 将新指针放置到套接字很容易,但现在每次连接结束时, socket和删除指针,所以我没有得到一个巨大的内存泄漏,对吧?我想通过设置这样做是确定: it = SocketList.begin while(it!= SocketList.end()){ if((* it) - > getClientId()== id){ pSocket = it; //< --------------编译器在这行输入 SocketList.remove(pSocket); pSocket-> Disconnect(); delete pSocket; break;但是编译器说的是这样: 错误:无法从类型'std :: _ List_iterator< MyTcp *>'转换类型'MyTcp *' 有人可以帮我吗?我认为我做的事情是正确的,不是一个迭代器在任何给定的时间只是指向集合的元素之一?如何解决?解决方案尝试: pSocket = * it; 迭代器很像指针,但实际上它们可以是指针或完全类行为像一个。在这种情况下,重要的是,当你解引用一个,你得到任何项目被存储在容器中。因为你在列表中存储 MyTcp * ,当你解引用迭代器,你会得到一个 MyTcp * 。 pSocket 的类型为 MyTcp * ,所以上面的赋值成功。你试图做的任务是不解除引用迭代器 - 你试图将迭代器本身分配给 pSocket 。 有以下情况: void foo() { MyTcp * array [10]; //一个包含MyTcp指针的数组 MyTcp ** iterator = NULL; //指针为数组创建好的迭代器(但不是std :: lists) for(iterator = array; iterator!= array + 10; ++ iterator) { //无法编译(不能将MyTcp **分配给MyTcp *: MyTcp * wrong = iterator; //这将成功: MyTcp * current = * iterator; // important取消引用迭代器} } Ok this time I decided to make a list using the STL. I need to create a dedicated TCP socket for each client. So everytime I've got a connection, I instantiate a socket and add a pointer to it on a list.list<MyTcp*> SocketList; //This is the list of pointers to socketslist<MyTcp*>::iterator it; //An iterator to the list of pointers to TCP sockets.Putting a new pointer to a socket was easy, but now every time the connection ends I should disconnect the socket and delete the pointer so I don't get a huge memory leak, right? well.. I thought I was doing ok by setting this:it=SocketList.begin();while( it != SocketList.end() ){ if((*it)->getClientId() == id){ pSocket = it; // <-------------- compiler complains at this line SocketList.remove(pSocket); pSocket->Disconnect(); delete pSocket; break; }}But the compiler is saying this: error: invalid cast from type ‘std::_List_iterator<MyTcp*>’ to type ‘MyTcp*’Can someone help me here? i thought I was doing things right, isn't an iterator at any given time just pointing to one of the elements of the set? how can I fix it? 解决方案 Try this:pSocket = *it; Iterators act a lot like pointers, but in reality they can be a pointer or a full-fledged class that acts like one. The important thing in this case, is that when you dereference one, you get whatever item is being stored in the container. Since you are storing MyTcp*s in the list, when you dereference the iterator you get a MyTcp*. pSocket is of type MyTcp* so the assignment above succeeds. The assignment you are trying to do is not dereferencing the iterator -- you are trying to assign the iterator itself to pSocket.It's kind of like the following case:void foo(){ MyTcp *array[10]; // An array full of MyTcp pointers MyTcp **iterator = NULL; // pointers make good iterators for arrays (but not for std::lists) for (iterator = array; iterator != array + 10; ++iterator) { // This fails to compile (cannot assign MyTcp** to MyTcp*: MyTcp *wrong = iterator; // This succeeds: MyTcp *current = *iterator; // important to dereference the iterator }} 这篇关于不是一个迭代器在c ++一种指针?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!
09-23 14:37