Re: pointer in queue
On Mon, 02 Feb 2009 20:39:57 +1300, Ian Collins <ian-news@hotmail.com>
wrote:
Carl Forsman wrote:
I have a deque object.
Can I insert pointers into the queue? (I heard I cannot insert pointer
into a queue)
like this:
===============
std::deque < Sock* > * socks;
socks = new deque< Sock* >;
Why use new here?
int port = 4322;
for ( int i = 0; i< 10; i++ ) {
Sock * sock = new Sock();
sock->Create1(this, port);
socks->push_back(sock);
}
later I will loop the queue of socket:
===============
for ( std::deque<Sock>::const_iterator iter = socks->begin(); iter !=
socks->end(); iter++ ) {
for ( std::deque<Sock*>::const_iterator iter = socks->begin();
iter != socks->end(); ++iter ) {
Sock temp = iter->first; // get the 1st socket
A deque iterator does not have a member first.
Unless you want to copy the object, you would require:
const Sock& temp = **iter;
Note the use of const reference. *iter is a Sock*.
Why i got compile error for the following? any idea?
for ( std::deque<Sock*>::const_iterator iter = socks->begin(); iter !=
socks->end(); iter++ ) {
const Sock& temp = **iter;
if ( temp.rfid.Compare(current_user) == 0 )
{
CString resXML = "<snd><cmd
id=\"snd_result\"><result>0</result></cmd></snd>\r\n";
char * resXML2 = new char[resXML.GetLength()+1];
_tcscpy(resXML2, resXML);
temp.WriteComm((LPBYTE)resXML2, (int)strlen(resXML2));
}
}
error C2663: 'CSocketComm::WriteComm' : 2 overloads have no legal
conversion for 'this' pointer