I created a class called person with two members name and age then I created two objects of that
class p1 and p2 and then I added them to a vector. I tried then to print them but could not.
this my code:
class Person{
public:
string name;
int age;
};
int main(){
Person p;
vector <Person> vector;
p.name = "Vitalik";
p.age = 29;
Person p2;
p2.name = "Bueterin";
p2.age = 50;
vector.push_back(p);
vector.push_back(p2);
for(int i = 0; i < vector.size(); i++){
cout << vector[i] << endl;
}
return 0;
}
I tried multiple ways to loop through the vector and print the elements but I keep getting this message:
error: invalid operands to binary expression ('std::ostream' (aka 'basic_ostream<char>') and 'std::__vector_base<Person, std::allocator<Person> >::value_type' (aka 'Person'))
cout << vector[i] << endl;