0

I use Codeblocks, this function to add a ShapePtr to a vector of ShapePtr doesn't add anything to the vector.

typedef Shape* ShapePtr;
void insertGrouped(vector<ShapePtr> arr, const ShapePtr &Sh)
{
    int flag = 0;
    if(arr.empty())
    {
        arr.push_back(Sh);
    }

    for(int i = 0; i < arr.size(); i++)
    {
        if(arr[i]->getType() == Sh->getType())
        {
            arr.insert(arr.begin() + i, Sh);
            flag = 1;
            break;
        }
    }
    if(flag == 0)
        arr.push_back(Sh);
}
  • 3
    You have to pass a reference to the vector, `vector& arr`. Now you pass by value and get a *copy* of the vector. The copy is updated, but not the original. – BoP Mar 26 '22 at 08:07
  • @BoP thanks.......been stuck for hoursssss lol – Subhranil Dey Mar 26 '22 at 08:10

0 Answers0