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
Please don't double post, it's ultimately a time waster for those who reply.

Always use code tags

http://www.cplusplus.com/articles/z13hAqkS/


I get an error.
What error? You should report it.
You said
It involves no pointers
, so I think the error is in line record[index] = NULL;.
However it seems wrong even if the compiler hadn't given you an error, so delete that line.
And, it would be better and simpler to use std::vector instead of an array:
1
2
3
4
5
6
7
#include <vector>
std::vector<myclass> record;

void WeighBridge::removeTruck(int index)
{
	record.erase(index);
}
@fcantoro

The other topic is here:

http://www.cplusplus.com/forum/beginner/177251/


Just trying to save you some time :+)
@Fcantoro, I included those headers what exactly do I write in "<my class>"?
I tried writing the WeighBridge class because it is the one that " Truck record[MAX]; as a private data member.

In the body of the method, I did as you coded but it underlines record as an error since is an object of type Truck.
This is the message:
Truck WeighBridge::record[40]
Error: expression must have class type

I have never used vectors before.
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?
AFAIK, myclass in your case is Truck.
I suggested you to use vector instead of array, because it is much easier to use, especially in tasks like removing an element.
But, we can't imagine your code.
You should post a minimal compilable code, so we will able to help you.
Topic archived. No new replies allowed.