If anybody could help me, I would be eternally grateful... I've been stuck on this issue for days:(
I'm attempting to have a class called "Client" that has a vector of pointers to pointers to "Files". When I push an element to the list, it appears to work fine, but when I add more than one, the entire vector gets overwritten with the most recent element.
so say "one" gets added to the vector, the vector is then "one".
but say you then add "two" to the vector, the vector is then "two two"...
I'm probably doing something obvious wrong, but I have been stuck here for so long I do not know what to do.
Here is where I add to my vector
1 2 3 4 5 6 7 8 9 10 11
void share( std::vector<Client*> *cliIP, int connfd, char* buf ){
File *file = new File(buf);
int place;
for (int i = 0; i < cliIP->size(); i++)
if( cliIP->at(i)->getConnfd() == connfd ){
cliIP->at(i)->addFile(file);
place = i;
}
cliIP->at(place)->printFiles();
}
here is my constructor:
1 2 3 4 5 6 7 8 9 10
Client::Client(constchar *IP, int portNum, int connfd){
_IPaddr = IP;
_portNum = portNum;
_active = 0;
_connfd = connfd;
_cache = 0;
_listOfClients = new std::vector<Client*>;
_listOfFiles = new std::vector<File*>;
}
Sorry I forgot to ask this sooner, but can I also see what the File class looks like? Also the File() constructor please. I have a few ideas what might be going on but I can't be certain without seeing those things first... Thanks.
Are you using the same buffer to feed to the file constructor? File only maintains a pointer to this buffer. It doesn't store a name or path. It only stores a pointer to an existing one. Any changes made to that buffer will be reflected when the pointers stored in File are dereferenced.
The use of pointers in your code looks gratuitous. Avoid using pointers when using or storing an object directly will serve.
File only maintains a pointer to this buffer. It doesn't store a name or path. It only stores a pointer to an existing one. Any changes made to that buffer will be reflected when the pointers stored in File are dereferenced.
That's what I suspected was going on - I couldn't really think of an eloquent way of phrasing it, so thank you for putting it into words!
The simple fix would be to just use std::strings instead. I see you've already included the <string> header, is there a reason you cant use it?