Remove a specific object from an existing list of object

I have a class called WeigglhBridge which has one of this method named removeTruck(int).

The class I mentioned above has an account object named Truck record[] which is an array of objects consisting of various trucks.

Now, getting to the main question, the removeTruck(int) method must receive an index as input parameter and remove an existing truck from the list, which is basically "record []".

It involves no pointers. It's just composition.
Please help me, I'm preparing for my exam.

Here is the code I've attempted:

void WeighBridge::removeTruck(int index)
{
for(int i=index; i<numberOfRecords; i++)
{
record[index] = record[index+1];
record[index] = NULL;
}
}

I get an error.
How do I remove object info without using pointers?

Kind regards.
Phillip K
Hi Phillip,

I'm going to take a stab at this... Since arrays are static, they can not change their size during run time. If you declare your array with 100 elements, it's going to stay 100 elements.
Therefor I don't think you can "Delete" an object in this manner, rather you set the value to zero.

That is a simple record[index] = 0;

If you want to physically delete the element you should be using vectors, or lists.
Vectors can be resized.
Lists you manage by changing 2 pointers.

I hope I understood your question properly, and I hope even more I got it right for you...


Last edited on
record[] is an object of Truck class.
It will store objects.

So when I code record[index] = 0; it gives me this error
"Error: no operator "=" matches these operands
operand types are: Truck = int;".

The problem statement im doing doesn't require pointers, that's what stumbles me.

I tried.. sorry about that.
In order to "remove" an element of an array, you need to move all elements at higher indexes down and decrement the variable you're using to keep track of the number of elements in the array.

1
2
3
4
5
6
7
8
9
void WeighBridge::removeTruck(int index)
{
    for(int i=index; i<numberOfRecords-1; i++)
    {
        record[index] = record[index+1];
        record[index] = NULL;
    }
    --numberOfRecords;
}
Last edited on
Wow, thanks a lot. Let me edit my method and I'll get back to you. I appreciate your help.

There's this other questions I'd like to ask too. I will however post them later.

Will inform you
That index will come with a value from the main to specify which truck I want to remove. So that particular truck must be removed.

I'm analysing your solution in depth in order to understand it.
That's not a hard concept. For example, if the truck you want to remove is number 63.

element 63 now == 64
element 64 now == 65
element 65 now == 66
element 66 now == 67
element 67 now == 68 and your last element will == itself. It's not deleted as much as over written. You just have to treat your array as if it had fewer elements. You could insert an object by reversing the process..

@Cire - Genius in it's simplicity. I wouldn't have thought of that in a hundred years.
@Cire, I came through this.
I forgot to mention that there's this method;

Truck WeighBridge::getVehicleAt (int index), which must receive an index as input parameter and return the truck St that index.

Now here is the catch:
I have call that method in the removeTruck method.

void WeighBridge::removeTruck(int Index)
{
//call the function that returns the object to be removed.

getVehicleAt(index);

}

So people how do I remove that object?
I have call that method in the removeTruck method.

Why? Should [tt]WeighBridge::removeTruck[tt] return the truck being removed?

So people how do I remove that object?

As I showed you.
This won't run on main properly in the main.cpp once I have more than one object.

How do I go about to calculate a fine for a specific overloaded truck at?

double WeighBridge::calcFine(string regNum[], double extraWeight[])
{

double fine = 0.0;
int i;

/*Not sure wheter to use counter or numberOfRecords.
This part stumbles me*/

for (i = 0; i < counter ; i++)
{
if (record[i].getRegistration() == regNum[i])
{
if (record[i].getVehicleCapacity() >= 38000) // 38000Kg equivalent to 38 TON
{
fine = extraWeight[i] * 0.90;
}
else
if ((record[i].getVehicleCapacity() >= 20000) /*20000kg equivalent to 20 TON*/ && (record[i].getVehicleCapacity() < 38000))
{
fine = extraWeight[i] * 0.70;
}
else /*10000kg equivalent to 10 TON*/
fine = extraWeight[i] * 0.50;
}
}

return fine;
}
Topic archived. No new replies allowed.