Class definition

Can anyone help me find whats wrong with this class definition looks fine to me

1
2
3
4
5
6
7
8
class Cohort
{ public:
	Cohort();
	void insert(Student x);
  private:
 	 vector <string> names;
 	 vector <vector<int> >marks(3, vector <int>());
};


vector <vector<int> >marks(3, vector <int>());

What's that 3 doing there?
trying to initiate a 2d vector so that there will be 3 empty vectors in which future adresses will correspond? I thought the 3 would do this?
If you are using C++11 you can do this if you use { } syntax (what is it called?)
vector <vector<int> >marks{3, vector <int>()};

otherwise you have to initialize everything in the constructor
Last edited on
cool safe. X
What's the matter with the 3? It constructs a vector of vector of ints with 3 elements. The only useless statements is the second argument, which is the same as the default argument, T().

http://cplusplus.com/reference/stl/vector/vector/

(And it's called an initializer list constructor)
(And it's called an initializer list constructor)


(Which cannot be done in the class definition.)
Last edited on
Yes, none will work. marks has to be initialized on the constructor. But you can move the expression to the constructor as it is:

1
2
3
4
5
Cohort::Cohort()
	: marks(3, vector<int>()) // use vector<int>() if you want, but it's not necessary
{
	// ..
}
Topic archived. No new replies allowed.