c++ - What does does a dynamic memory request cause error? -
class tower { public: unsigned int no; // error! int *levels = new int[no]; // error disappears when comment out line tower(int init) {no = init;} }; i made class declaration show above, , reason, doesn't work. error message shows is: invalid use of non-static data member 'tower::no'. why? i'm not sure going on.
can help?
thanks!
in tower.h :
class tower { public: tower(int size); // note we're missing destructor // should invoke delete[] on _levels. unsigned int _size; int *_levels; // consider sort of smart pointer // later when continue learn c++, // or use std::vector! }; in tower.cpp :
tower::tower(int size) : _size(size), _levels(null) { if(_size > 0) { _levels = new int[size]; } } check out examples online too.
Comments
Post a Comment