Add element to a vector of objects?

Hi! I've just started with Classes and what I want to do is to create a new instance whenever the user presses x key. For that, I created a vector, but the problem is I can't find the way to add a new element at the end of it. Here's my code:
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
#include<iostream>
#include<string>
#include<vector>
using namespace std;
class Person{
	int age;
	string name;
public:
	Person(){
	age=0;
	name="\0";}
	void setValues(){
		cin>>name>>age;
	}
	void showName(){cout<<name<<endl;}
	void showAge(){cout<<age<<endl;}
};
int main(){
	vector<Person> People(2);
	People[0].setValues();/*This is just for testing*/
	People[0].showName();
	People[0].showAge();
	People[1].showName();
	People[1].showAge();
	Peolpe.push_back(/*I don't know what to do here*/);
}

Another question, which I guess is really stupid, is why do I get an error if I declare "Person", before main, and define it after main, like this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include<iostream>
#include<string>
#include<vector>
using namespace std;
class Person;
int main(){
	vector<Person> People(2);
	People[0].setValues();
	People[0].showName();
}
class Person{
	int age;
	string name;
public:
	Person(){
	age=0;
	name="\0";}
	void setValues(){
		cin>>name>>age;
	}
	void showName(){cout<<name<<endl;}
	void showAge(){cout<<age<<endl;}
};


Thanks!
People.push_back(Person());

class Person; This only allows you to have pointer and references to Person objects. To be able to create Person objects, call its member functions and constructors or in any other way use Person you need to define it before.
Normally when you define a class object you'd do it like this:
1
2
class MyClass { /*...*/ };
MyClass MyObj();


This creates MyObj with the default constructor of MyClass.

Now if you were to do something like this:
MyClass(); That creates and returns the reference to the object.

That lets you do dynamic memory allocation such as what you are trying to do with vectors, or also lets you use pointers like so:
1
2
3
class MyClass { /*...*/ };
MyClass* MyObjPointer;
MyObjPointer = new MyClass();


Now what if we don't want to use a default constructor? Lets say we have:
1
2
3
4
5
class MyClass {
    int _a;
public:
    MyClass(int a) : _a(a) {}
};


We can now just call that constructor (or any constructor) by using the appropriate arguments:
1
2
MyClass* MyObjPointer;
MyObjPointer = new MyClass(5);

or
1
2
3
std::vector<MyClass> MyVector;
MyVector.push_back( MyClass(5) );
MyVector.push_back( MyClass(1) );
MyClass MyObj();
This creates MyObj with the default constructor of MyClass.

No that declares a functions named MyObj with MyClass as return type.
you can also use initializer lists, to initialise the object without using the costuctor explicitly:
People.push_back({123, "Foo Bar"})
Or, sice I see you are using c++11, you can use emplace_back for the same functionality:
people.emplace_back(123, "Foo Bar")
You choose which one of the three you will use.
Topic archived. No new replies allowed.