create a vector of instances of a class in c++
The main problem is you are trying to execute a for loop at global scope. It is acceptable to define and initialize variables outside of a function, but using a for loop or assignment operator is not. Put the for loop into main() (and I would recommend you also put N and the vector/student array into main() and everything should work.
Additionally, the compiler is complaining because when you declare Student array[5];
or vector<Student> ver[N];
it is looking for a default constructor for Student called Student(), which just sets default values for a class. You need to provide this inside the Student class; set the id to some value that can never be an actual student ID, something like -1.
Option #1:
You should replace vector <Student> ver[N]
with vector<Student> ver(N)
The std::vector is a class, that represents the vector by himself, you shouldn't create an array of vectors, you should just pass N(vector size) to it's constructor. Check this link
Option #2:
Student ver[N];
is incorrect, since the Default Constructor Student() is invoked N times, but you haven't implement it.
So you have to use array initilizer Student ver[5] = {1, 2, 3, 4, 5}
or implement the default constructor explicitly.
And of course - the "for" loop has to be used inside function body.
This:
vector <Student> ver[N];
Creates an array of N
elements. Each element is vector<Student>
. This is not you want. You were probably trying to create a vector of N
elements. The syntax for this is:
vector <Student> ver(N);
But you can't use this because your class does not have a default constructor. So your next alternative is to initializae all the objects with the same element.
vector <Student> ver(N, Student(0));
You also tried to create an array of students like this:
Student ver[N];
This will not work. Because it tries to initialize every element in the array with the default constructor. But your class does not have a default constructor. So this will not work. But this is why your original code did work:
Student ver_list[2] = {7, 9}; // Here you are using the constructor for your object.
// It uses the normal constructor you provided not the default one.
The other issues is that you can not run code outside a function(method).
So this will not work:
for(unsigned int i = 0; i < N; ++i )
ver[i].set_id(i);
In C++11 you can initialize a vector the same way as an array:
vector<Student> ver = { 0, 1, 2, 3, 4, 5};
If you don't have C++11 or initialization is more complex. Then you need to write a wrapper.
class VecWrapper
{
public:
std::vector<Student> ver;
VecWrapper()
{
ver.reserve(N);
for(unsigned int i = 0; i < N; ++i )
ver.push_back(Student(i));
}
};
Now You can place this in global scope and it will auto init.
VecWrapper myData; // myData.vec initializaed before main entered.
int main()
{}
Full solution:
Option 2:
#include<iostream>
#include<vector>
using namespace std;
const unsigned int N = 5;
// The following is not correct
// This creates an arrya of `N` elements each element is `vector <Student>`
//
// vector <Student> ver[N]; // Create vector with N elements
//
// The following lines are not allowed.
// All code has to be inside a function.
//
// for(unsigned int i = 0; i < N; ++i )
// ver[i].set_id(i);
// What you want is:
// I use the following because it is unclear if you have C++11 or not.
class VecWrapper
{
public:
std::vector<Student> vec;
VecWrapper()
{
vec.reserve(N);
for(unsigned int i = 0; i < N; ++i )
vec.push_back(Student(i));
}
};
VecWrapper myData; // myData.vec
int main()
{
cout<< "Hello, This is a code to learn classes"<< endl;
cout<< myData.vec[1].get_id() << endl;
return 0;
}