c++ - Use the functions of a vector objects using a pointer -
1).why did error? correct syntax?
2).is there way write same code without using library "vector"?
#include <vector> myclass() { public: myclass(int x,int y); void dothis() { //something } } int main(void) { std::vector<myclass>*ex_vector = new std::vector<myclass(5,myclass{10,10}); ex_vector[0]->dothis(); //error here delete []ex_vector; }
i error:
error: base operand of '->' has non-pointer type 'std::vector<myclass>'
the correct syntax is
(*ex_vector)[0].dothis();
also, should delete ex_vector;
, not delete[] ex_vector;
since type of new
not raw array type.
however, there's reason new
std::vector
. use plain object.
Comments
Post a Comment