Remove object from an array in C++? -
here's simplified sample of code : .h
class company { public: company(); void addemployee(const employee &emp); void removeemployee(); private: employees *listemployees; };
the .cpp
company::company(){ listemployees = new employees[16]; } company::addemployee(const employee &emp) {listemployee[index]=emp;} company::removeemployee(){ ??? }
i remove employee stored in array. tried use :
delete listemployee[index]; //->"cannot delete expression of type 'employees' listemployee[index]=null; //->"no viable overloaded '='"
i didn't find satisfying solution on web. also, i'm not familiar pointer , reference, maybe error cames this. thanks.
edit : i'm not allowed use vectors, must use arrays.
edit2 : answer. here's solution used :
for(int i=indexemp;i<sizearray-1;i++){ listemployees[i]=listemployees[i+1]; }
where indexemp index of employee want remove.
there no built-in way remove element c++ array. arrays have fixed size, can't add or remove elements.
you have other options. example, use std::vector
type, acts array lets add , remove elements. alternatively, if need store elements in some order , don't care order is, try using std::unordered_map
or std::map
.
hope helps!
Comments
Post a Comment