c++ - Working with a vector of a class -
i newbie @ c++, , starting work container called classes.
i working on program object defined, , stored inside vector.
the program follows, , description of error got.
this "car.h"
#ifndef car_h_guard #define car_h_guard class car{ int v;//the velocity int loc;//location public: //default constructor, copy constr., cp. assgnmt. optr., destructor car(); car(const car& j); car& operator=(const car& j); ~car(); void set_v(int); //sets velocity void set_pos(int); //sets position int get_v (){return((v));}//returns velocity int get_pos(){return(loc);}//returns position }; #endif
this "car.cpp"
#include "car.h" #include <iostream> using namespace std; //constructor car::car() { v=1; loc=0; } //copy constructor car::car(const car& j){ v=j.v; loc=j.loc; } //copy assignment operator car& car::operator=(const car& j){ v=j.v; loc=j.loc; return *this; } //destructor car::~car () { delete &v; delete &loc; } //sets velocity void car::set_v(int p){ v = p; } //sets position void car::set_pos(int l){ loc = l; }
i have tried create "move constructor", having copy assignment operator. following code involving vector of objects work.
#include "main.h" #include "car.h" #include <iostream> #include <vector> using namespace std; //update car position void update (vector <car> &cr){ //changes state of cars, eg. (int c = 0; c<cr.size();c++){ cr[c].set_v(1); cr[c].set_pos(100); } } int main(){ //a vector of cars vector<car> cat; car j; j.set_v(100); j.set_pos(99); cat.push_back(j); //new cars enters road j.set_v(120); j.set_pos(77); cat.push_back(j); j.set_v(80); j.set_pos(55); cat.push_back(j); //updating cars update(cat); //output (int = 0; i<cat.size(); i++){ cout << cat[i].get_v()<<" velocity of car "<<i<<".\n"; cout << cat[i].get_pos()<<" position of car "<<i<<".\n"; } }
and following error, looked on internetz.
main(8852) malloc: * * * error object 0x7fff5fbff630: pointer being freed not allocated * * * set breakpoint in malloc_error_break debug abort trap
although might compiler incompability (i wrote in xcode... , uses g++, on mac 10.6.8 ...), code give output when there 1 car in vector. when tried introduce other cars, receive malloc error. wondering whether missing class definition, or somehow botched data-types, or pointer referencing. there issue values outputted if tried non-trivially manipulate update function.
i have tried pass instead vector of pointers (to class) update function; still have trouble inserting new elements vector. site helpful in helping me learn class structure, , pointers.
thanks reading long problem.
one uses delete
on things 1 has used new
. never delete
wasn't separately allocated new
. these lines in car
destructor are... destructive (pardon pun):
delete &v; delete &loc;
delete these lines -- they're totally unnecessary , they're source of problem. time you'd use delete
in destructor when you've used new
allocate object in constructor -- or when pointer-to-object passed constructor understanding object responsible deleting pointer when object destructed.
Comments
Post a Comment