0

I have a vector containing pointers to several classes. The classes are defined as so:

class trackerSocket{
public:
    ~trackerSocket();

    int trackerInitialize(string address);
    int trackerSend(string getParams);
    int trackerRecv();

    be_node *responseDict;
    bool working;
    unsigned int interval;
    string trackerid;
    unsigned int complete;
    unsigned int incomplete;
    be_dict *peersDict;
    string peersBinary;
    bool dictionary;

private:
    string address;
    string port;
    string protocol;
    string page;
    SOCKET ConnectSocket;

    int parseAnnounce(string announce);
    int parseTrackerResponse(string response);
};

the vector is declared by this line:

vector<trackerSocket*> trackers;

and the classes are added to the vector using this line:

trackerSocket *temptracker = new trackerSocket();
//Initialize values in temptracker structure here (omitted)
trackers.push_back(temptracker);
//Reset temptracker
temptracker = new trackerSocket();
//Initialize values in temptracker structure here (omitted)
trackers.push_back(temptracker);
//Repeat

How can I access the working variable of each class in the vector? The following code does not print working at all even though I know some of the classes have it set to true.

for(i = 0; i < trackers.size(); i++){
    if(trackers[i]->working){
        printf("Working.\n");
    }
}

Thanks for your help :)

brnby
  • 1,433
  • 1
  • 19
  • 35
  • This is technically fine - you must have some problem elsewhere. – Georg Fritzsche Aug 06 '12 at 11:55
  • 2
    Either `working` is actually `false` for all elements, or `printf` doesn't do any printing or `trackers` is empty. – Andreas Brinck Aug 06 '12 at 11:55
  • Hmmmm. By messing around with the watch feature in visual studio it turns out I can access the first class in the vectors working value using this code: (*((trackers)._Myfirst[0x00000000])).working and the second using this code: (*((trackers)._Myfirst[0x00000001])).working , does this help you guys to answer the question? – brnby Aug 06 '12 at 12:07
  • 2
    You should post a minimal example that reproduces the problem. The code looks fine. – juanchopanza Aug 06 '12 at 12:18
  • Whilst trying to do what you suggested the program seems to work now. I have NO idea whether it was something I have done or something else but thanks for all of your time anyway :) Sorry to have wasted it :( – brnby Aug 06 '12 at 14:29

2 Answers2

0

Try switching the line where you are allocating memory and the line where you are using push_back(). Insertion may invalidate pointers in std::vector.

Pointers to elements of std::vector and std::list

Community
  • 1
  • 1
ApplePie
  • 8,814
  • 5
  • 39
  • 60
0

If think issue may be in scope in which initialization of instances of classes is perofrmed. As I know instances and therefore their members are initialized by zero values in namespaces, in static and in global scope, if you create instances of object on one of this scope this can lead to that filed working is init by default value "0" and I don`t wonder why

for(i = 0; i < trackers.size(); i++){
    if(trackers[i]->working){
        printf("Working.\n");
    }
} 
not working as you expect. If so I offer you to use default constructor.
spin_eight
  • 3,925
  • 10
  • 39
  • 61