First of all it has to be
list.push_back(variable)
instead of
list.pushback<variable>
The difference is that in case 1 you create a pointer to the variable, which means you only store the adress of variable in the list. This code
#include <string>
#include <vector>
#include <iostream>
using namespace std;
struct Data {
string first;
string middle;
string last;
int age;
};
int main()
{
vector<Data*> list;
Data* variable;
variable = new Data;
list.push_back(variable);
cout << list[0];
cin.get();
return 0;
}
would only return you the address of the place in memory where variable was stored.
So to return some value of variable you could use something like
vector<Data*> list;
Data* variable;
variable = new Data;
variable->setage(5);
list.push_back(variable);
cout << (*list[0]).getage();
cin.get();
return 0;
}
Where *list[0] dereferences the pointers, that means you get the value and not the adress of it.
If you work without pointers instead
vector<Data> list;
Data variable;
list.push_back(variable);
instead, you would store a copy of variable in the list.
So in this case you could directly address variable by something like
list[0].getage()
if you create a getage() function in the struct Data.
If you don't know how to do so, an easy( maybe not the best) way is to add
public:
int getage(){
return age;
}
void setage(int x){
age = x;
}
};
in your struct Data.