c++ - Vector fails to get initialized -
i trying initialize vector of self-defined type datacard
:
vector<datacard> dataset; dataset.reserve(200);
however compiler doesn't allow me indicating error @ sec line (where seek reserve space container):
required here
however when alter declaration following
vector<datacard*> dataset;
it starts working.
what reason compiler not allowing me utilize type is, letting me utilize pointer type? need vector of objects, not pointers them.
edit
header file of datacard
#include <card.h> class datacard { char* name; // name according filename char class; // classes: 2, 3, 4, 5, 6, 7, 8, 9, t, j, q, k, double distancetoquery; public: void resetdistance(); int numblacktotal (bitmap* bmp); bitmap image; int numofblackpixels; char* getname(); void setname(char* n); void setclass (char* c); char* getclass(); void setdistance(double distance); double getdistance (); datacard(const char* fname); datacard(); virtual ~datacard(); };
here's error message compiler
/usr/include/c++/4.7/bits/stl_uninitialized.h:77:3: required ‘static _forwarditerator std::__uninitialized_copy<_trivialvaluetypes>::__uninit_copy(_inputiterator, _inputiterator, _forwarditerator) [with _inputiterator = datacard*; _forwarditerator = datacard*; bool _trivialvaluetypes = false]’ /usr/include/c++/4.7/bits/stl_uninitialized.h:119:41: required ‘_forwarditerator std::uninitialized_copy(_inputiterator, _inputiterator, _forwarditerator) [with _inputiterator = datacard*; _forwarditerator = datacard*]’ /usr/include/c++/4.7/bits/stl_uninitialized.h:260:63: required ‘_forwarditerator std::__uninitialized_copy_a(_inputiterator, _inputiterator, _forwarditerator, std::allocator<_tp>&) [with _inputiterator = datacard*; _forwarditerator = datacard*; _tp = datacard]’ /usr/include/c++/4.7/bits/stl_vector.h:1112:8: required ‘std::vector<_tp, _alloc>::pointer std::vector<_tp, _alloc>::_m_allocate_and_copy(std::vector<_tp, _alloc>::size_type, _forwarditerator, _forwarditerator) [with _forwarditerator = datacard*; _tp = datacard; _alloc = std::allocator<datacard>; std::vector<_tp, _alloc>::pointer = datacard*; std::vector<_tp, _alloc>::size_type = long unsigned int]’ /usr/include/c++/4.7/bits/vector.tcc:76:70: required ‘void std::vector<_tp, _alloc>::reserve(std::vector<_tp, _alloc>::size_type) [with _tp = datacard; _alloc = std::allocator<datacard>; std::vector<_tp, _alloc>::size_type = long unsigned int]’ /home/ppsadm01/documents/vgv/src/main.cc:145:22: required here /usr/include/c++/4.7/bits/stl_construct.h:85:7: error: no matching function phone call ‘datacard::datacard(const datacard&)’
as error message suggests need implement re-create constructor datacard::datacard(const datacard&)
. need because illustration vector<datacard>::push_back()
re-create datacard
instances.
when write re-create constructor, careful all fellow member variables copied - in right way. illustration implementation be
datacard::datacard(const datacard &rhs) { strcpy(name, rhs.name); class = rhs.class; distancetoquery = rhs.distancetoquery; image = rhs.image; // create sure ok! }
c++ pointers vector
No comments:
Post a Comment