Accessing data from a vector of objects
<p>I have read in data from a file and want to store the data as a vector of objects.</p>

<pre><code>vector &lt;Thing*&gt; thingVector;
for (int i = 0; i &lt; 10; i++) {
// Read in contents of file
getline(fileName, v1, ',');
cout &lt;&lt; v1 &lt;&lt; endl;
getline(fileName, v1, ',');
cout &lt;&lt; v2 &lt;&lt; endl;
getline(fileName, v3, ',');
cout &lt;&lt; v3 &lt;&lt; endl;
getline(fileName, v4, '\n');
cout &lt;&lt; v4 &lt;&lt; endl &lt;&lt; endl;
// Store
Thing* thingDetails = new Thing(v1, v2, v3, v4);
thingVector.push_back(thingDetails);
delete thingDetails;
}
thingFile.close();
cout &lt;&lt; "Size of THING vector is " &lt;&lt; thingVector.size() &lt;&lt; endl; // Displays 10

cout &lt;&lt; thingVector[0].getV1 &lt;&lt; endl; // ERROR HERE
</code></pre>

<p>How can I store each record in the vector and then access the data?</p>

<p>I also tried to do it like this:
    thingVector.push_back(Thing(v1, v2, v3, v4));</p>

<p>I didn't have the last and third to last lines in the for statement when I tried it like this, but I couldn't access the data so gave up on this method.</p>

<p>Any suggestions?</p>

<p>THING .H FILE</p>

<pre><code>#ifndef THING_H
#define THING_H

#include &lt;string&gt;

using namespace std;

class Thing {
public:

Thing(string v1, string v2, string v3, string v4);
string getV1();
string getV2();
string getV3();
string getV4();

private:
string v1;
string v2;
string v3;
string v4;
};

#endif
</code></pre>

<p>THING .CPP FILE</p>

<pre><code>#include "thing.h"

#include &lt;string&gt;

using namespace std;
Thing::Thing(string aV1, string aV2, string aV3, string aV4) {
v1 = aV1;
v2 = aV2;
v3 = aV3;
v4 = aV4;
}

string Thing::getV1(){
return v1;
}

string Thing::getV3(){
return v2;
}

string Thing::getV3){
return v3;
}

string Thing::getV4(){
return v4;
}
</code></pre>
