Vector overwritten every time ->push_back() is used

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(const char *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*>;

}

here is addFile:
1
2
3
void Client::addFile( File *file ){
  _listOfFiles->push_back(file);
}
Last edited on
Hmm, can we see Client::printFiles() ?
1
2
3
4
5
6


for (std::vector<File*>::iterator iter = _listOfFiles->begin(); iter !=_listOfFiles->end(); ++iter )
  (*iter)->print();



also for what it's worth, I've attempted to print every which way... It prints the same funky little vector:P
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.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21

#ifndef _FILE
#define _FILE
#include <string>

class File{

 public:
  File(char *name);
  ~File();
  char * getName();
  char * getPath();

 private:
  char * _name;
  char * _path;

};

#endif


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22

#include "file.h"

File::File(char * name){
  _name = name;
  _path = name; // going to fix later

}

File::~File(){

}

char * File::getName(){
  return _name;

}

char * File::getPath(){
  return _path;
}
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?
Topic archived. No new replies allowed.