c++11 - How can I push back onto a vector an instance of a class in C++ -


bear me please, first time posting. have 3 classes. class suppliers has set of class parent. class parent has vector of class location , class location has data memebers. ex (this pseudo code, not actual code. i've shown simplicity sake):

class suppliers{ set<parent> setter; }; class parent{ vector<location> loc; }; 

`

the following constructor of location class created. run no problems until lines hit 2 lines iterators. trying find specific parent , push new location onto parent 'loc' vector. pass in iterator i've found reference. try push new instance of location class following error.

data.cpp:139:33: error: passing 'const std::vector' 'this' argument of 'void std::vector<_tp, _alloc>::push_back(const value_type&) [with _tp = location; _alloc = std::allocator; std::vector<_tp, _alloc>::value_type = location]' discards qualifiers [-fpermissive]

the last line gives error says cannot alter read-only object. i'm confused why read object. did research , thought needed add copy constructor. however, caused more problems solved. ideas?

location::location(set<parent>::iterator &it, vector<string> v){     sup_id = v[0];  address1 = v[2];  address2 = v[3];  city = v[4];  state = v[5];  country = v[6];  zip = v[7];  ((*it).loc).push_back(*this);  ((*it).num)++;    } 

the problem set sorted. if you'd allowed change element through iterator, mean potentially invalidate iterator , therefore since c++11, iterator of set constant bidirectional iterator , thereby has same semantics set's const_iterator.

the solution, although ugly, remove, modify , re-insert element. there proposal allow modification of keys of associative containers, don't know if there progress in getting standardized.


Comments

Popular posts from this blog

python - TypeError: start must be a integer -

c# - DevExpress RepositoryItemComboBox BackColor property ignored -

django - Creating multiple model instances in DRF3 -