c++ - error while extending an array: no operator found which takes -
i want extend array 1 when call addbranch function, , add new branch object extended empty area, , trying prevent code memory leak. thought doing right stuck function.
it gives me error "binary '=' : no operator found takes right-hand operand of type 'branch *' (or there no acceptable conversion)". need here assigning last element new branch object won't deleted after extermination of function. new c++, there might big mistakes. not allowed use vectors etc.
// ------- adds branch system -------- // void bankingsystem::addbranch(const int id, const string name){ if(isbranchexisting(id)){ cout << "\n\tbranch " << id << " exists. please try id number."; } else if(!isbranchexisting(id)){ branch* temparray = new branch[cntaccounts]; for(int = 0; i<cntaccounts; i++){ temparray[i] = allbranches[i]; } delete[] allbranches; allbranches = new branch[cntaccounts+1]; for(int = 0; i<cntaccounts; i++){ allbranches[i] = temparray[i]; } allbranches[cntaccounts] = new branch(id, name); // error @ line } }
as error message says, you're trying assign pointer object. (probably) want assign object instead:
allbranches[cntaccounts] = branch(id, name); // no "new"
i suggest use std::vector<branch>
rather hand-forged arrays. fix memory leak forgetting delete temparray
, or throwing exception if add missing delete[]
.
also, if use vector
, whole dance can replaced with
allbranches.push_back(branch(id, name));
Comments
Post a Comment