You correctly used ->
in print1
instead of .
(like in print
) to operate on the pointer to the vector. This works because x->y
is equivalent to (*x).y
, meaning you correctly dereferenced the pointer to the vector before accessing it.
Except for []
. Here you also have to dereference the pointer before using []
. So:
cout << (*v)[x] << endl;
There is no abbreviation (also called "syntactic sugar") for (*x)[]
like there is for (*x).y
, so you must do it manually.
The error message is confusing because using []
on a pointer is valid - x[y]
is equivalent to *(x+y)
, which means you are doing pointer arithmetics: You use v
as if it was a (C-style) array of vectors, and you try to get the x
th element from this array of vectors. Lucky for you, the compiler doesn't know how to <<
a Vector
with cout
- but if it could, the code would compile and do something you (probably) did not intend.