-
November 27th, 2014, 06:26 AM
#1
Erorr asigning pointer to iterator ( only after porting to VS2013 )
Hello, im trying to port a code from vc6 to vs2013 and im having this error
Code:
Error 11 error C2440: 'initializing' : cannot convert from 'char *' to 'std::_Vector_iterator<std::_Vector_val<std::_Simple_types<char>>>'
on this line
Code:
vector<char>::iterator BufIt = (char*)lpBuffer;
what i do with this is to stack fragments of data of type char* coming from a socket in buffer to a vector that acts as buffer, I do this since I transfer big chunks of data and the data gets fragmented by the nature of the sockets, I stack the data once its complete I retrieve the final result from the vector.
this code worked flawlessly for long time but now Im trying to port and compiler throws this error, whats the new way to assign a char array pointer to a iterator so i can stack it in the vector.
Thx in advance.
-
November 27th, 2014, 08:26 AM
#2
Re: Erorr asigning pointer to iterator ( only after porting to VS2013 )
So, you need to add some buffer (char*) to vector. I don't see any reason to use vector iterator for reading plain char array. Please show some more code to get an advice how to rewrite it. For example, to add buffer to vector you can use something like this:
Code:
void Add(vector<char>& v, const char* data, size_t length)
{
size_t size = v.size();
v.resize(size + length);
memcpy(&v[size], data, length);
}
Last edited by Alex F; November 27th, 2014 at 08:36 AM.
-
November 27th, 2014, 08:53 AM
#3
Re: Erorr asigning pointer to iterator ( only after porting to VS2013 )
First of all thx for your time this is the code on the data arrival callback produced by the socket handler a wrapper Im using, it works flawlessly on vc6 the problem is the compiler is outdated and also the IDE is too old...
Stacking the buffer manually has bough me tons of troubles that's why i ended using a vector for handling binary buffers, and that's why i try to avoid that practice.
the code is complex and extent but what i need is simple, the data arrives fragmented, the vector takes the size of the fragment and then it proceeds to stack it, once the size stored on the header is reach, takes it off from the vector, all i want is to do the same thing in the new compiler it has no problems and i rather to not change it unless is necessary to compile.
Code:
void CSocketComm::OnDataReceived(const LPBYTE lpBuffer, DWORD dwCount){
int PacketID=0;
int MinLen=0;
Progreso=Progreso+dwCount;
//Declaramos el Iterator
vector<char>::iterator BufIt = (char*)lpBuffer;
//Apilamos la data entrante
IncBuffer.insert(IncBuffer.end(), BufIt, BufIt + dwCount);
if(IncBuffer.size()<16 && PckType==0){//nos aseguramos que almenos haya sig+size y que ademas no haya una transferencia de packets ni de archivos en progreso
return; //Salimos
}
Packet* Pck = (Packet*)&IncBuffer[0];
//if(PckType==0){ //AHORRA RECURSOS PERO PODRIA CAUSAR DEAD LOCK
if(strncmp(Pck->Sig,"FLESENT",7)==0){ //Envio De Archivos
PacketID=1;
MinLen=(int)Header;
}else if(strncmp(Pck->Sig,"DTPACKT",7)==0){//Data Packet
PacketID=2;
MinLen=24;
}
//}
if(IncBuffer.size()<MinLen){ //una vez q sabemos que tipo de packet estamos manejando nos aseguramos q se cumpla su tamaƱo
return;
}
if(PacketID==1){ //Envio De Archivos
resuming what i need is to
stack LPBYTE lpBuffer which is DWORD dwCount of lenght to the end of the IncBuffer vector
IncBuffer.insert(IncBuffer.end(), BufIt, BufIt + dwCount);
Last edited by Alphadan; November 27th, 2014 at 09:01 AM.
-
November 27th, 2014, 09:13 AM
#4
Re: Erorr asigning pointer to iterator ( only after porting to VS2013 )
OK, you can replace IncBuffer.insert with the code from my first post. Is there some problem with this?
-
November 27th, 2014, 09:14 AM
#5
Re: Erorr asigning pointer to iterator ( only after porting to VS2013 )
Originally Posted by Alphadan
vector<char>::iterator BufIt = (char*)lpBuffer;
that code is and has always been wrong, because std::vector iterators are guaranteed to be random access iterators and nothing more ( as you noticed, this includes the possiblity for an STL implementation to just use a plain pointer as was the case in vc6 ).
fortunately there's no need of that cast, because vector::insert accept *any* input iterator range ( hence including a range of char* )
Code:
//Declaramos el Iterator
// vector<char>::iterator BufIt = (char*)lpBuffer; -- NOPE: don't do it !
IncBuffer.insert(IncBuffer.end(), lpBuffer, lpBuffer + dwCount);
-
November 27th, 2014, 04:53 PM
#6
Re: Erorr asigning pointer to iterator ( only after porting to VS2013 )
Thx you both will try that, i think that solves my problem !!
added reputation for you both
Last edited by Alphadan; November 27th, 2014 at 04:56 PM.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|