Iterating Over a Vector of Objects

Greetings all,

I have a question regarding iterating over a vector that holds objects. For some reason, the code within the for loop is not printing to the console. Does anyone know why?

I've included the header and class files for the object I've made as well. If there are any other comments regarding the use of methods, design, etc.. please feel free to comment. Constructive criticism is always welcome!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
#include <iostream>
#include <Doctor.h>
#include <vector>

using namespace std;

int main()
{
    string name = "Bob";
    Doctor doc1 (name);
    Doctor doc2;

    cout << "doc1 name: " + doc1.getName() << endl;
    cout << "doc2 name: " + doc2.getName() << endl;

    doc1.setID(2019);
    doc2.setName("Alice");
    doc2.setID(2020);

    // My attempt to use a vector of objects.
    std::vector<Doctor> docList;
    std::vector<Doctor>::iterator iter;

    // Is this the correct way to insert objects into the vector?
    docList.insert(iter, doc2);
    docList.insert(iter, doc1);

    /* For some reason, the name of each Doctor object is not printing
     * in the for loop below */
    for( int i = 0; i < docList.size(); i++){
        string name = docList[i].getName();
        cout << name << endl;
    }

    return 0;
}            // end main

#ifndef DOCTOR_H
#define DOCTOR_H

#include <string>

class Doctor
{

    private:
        std::string name;
        int id;

    public:
        Doctor();
        Doctor (std::string n);
        Doctor (std::string n, int num);
        //virtual ~Doctor();

        std::string getName(){return name;}
        void setName(std::string);

        int getID(){return id;}
        void setID(int num);


};

#endif // DOCTOR_H

// Beginning doctor.cpp file below:

#include "Doctor.h"

using namespace std;
/*
This is the Doctor cpp file where the Doctor class's
functions are implemented.
*/
string name;
int id;

Doctor::Doctor()
{
    name = "BLANK NAME";
    id = 99999;
}

Doctor::Doctor(string n)
{
    name = n;
}

Doctor::Doctor(string n, int fig)
{
    name = n;
    id = fig;
}

/*Doctor::~Doctor()
{
    //dtor
}*/

string getName(){ return name;}

void Doctor::setName(string n){ name = n;}

int getID() {return id;}

void Doctor::setID(int fig){ id = fig;}

// end of Doctor.cpp
Last edited on
Does anyone know why?

Most likely because the program is crashing.

By far, the most common way to put objects into vectors is with push_back, e.g.:
docList.push_back(doc1);

insert is for inserting objects at particular locations within the vector.

See this tutorial:
https://cal-linux.com/tutorials/vectors.html

The snippet
1
2
docList.insert(iter, doc2);
docList.insert(iter, doc1);

has two problems:
a.) iter is not initialized to refer to a particular position within the vector; and
b.) insert makes all existing iterators invalid (in general).
Last edited on
Thank you, mbozzi. That was exactly the problem!

Interesting that this was causing the program to crash. I wonder what is going on there to make the program crash?
Reallocation invalidates iterators. See http://www.cplusplus.com/reference/vector/vector/insert/
Topic archived. No new replies allowed.