on the fly object initialization c++ -


i'm trying used c++. want add object in dynamic array each time read line file without knowing dimension.

i declared pointer array this:

rlmr *myarray; 

where rlmr class public string id.

now read file line line , after want add object myarray

        index = 0;         while (fgets(buffer, maxsizebuffer, fp) != null) {             if(buffer[0] == '#') // skip comment lines                 continue;             else {                 sscanf(...);                 index++;             }         // @ point want new object in array         myarray = (rlmr*) malloc(sizeof (rlmr) * index);         // here try call object constructor passing id         myarray[index-1] = new rlmr(cbeacid);         } 

i don't understand error compiler:

error: no match âoperator=â in â*(myarray+ ((unsigned int)(((unsigned int)index) * 28u))) = (operator new(28u), (<statement>, ((rlmr*)<anonymous>)))â

what wrong. and, how done using std::vector. i'd understand both ways, thanks.

what wrong? new returns pointer. in following line trying assign pointer existing object:

myarray[index-1] = new rlmr(cbeacid); 

instead should write:

new (myarray + index - 1) rlmr(cbeacid); 

which called "placement new" (cf here). solves problem, should not satisfy anyone.

second, how vector:

std::vector<rlmr> data;  while (fgets(buffer, maxsizebuffer, fp) != null) {     if(buffer[0] == '#') // skip comment lines         continue;     else {         sscanf(...);     }     data.emplace_back(cbeacid); } 

details on vector eg. vector::emplace_back available here.


Comments

Popular posts from this blog

php - How to add and update images or image url in Volusion using Volusion API -

javascript - IE9 error '$'is not defined -