Some vector based questions.

I would like to know why cant I delete custom objects from my vector and why am I geting a warning when applying a method to all of the objects in a vector?
(I'm using Code::blocks & mingw.)

1
2
3
4
5
6
7
8
9
10
void SimulatorClass::UpdateSimulation(float dT)
{
    if (!cars.empty())
    {
        for (int i = 0; i <= cars.size(); i++)
        {
            cars.at(i).update(dT);
        }
    }
}

gives a warning like this:
"comparison between signed and unsigned integer expressions [-Wsign-compare]"

1
2
3
4
5
6
7
8
9
10
11
12
void SimulatorClass::FreeSimulation()
{
    if (!cars.empty())
    {
        for (int i = 0; i <= cars.size(); i++)
        {
            delete cars.at(i) ;
            cars.at(i) = NULL;
        }
    }
    cars.clear();
}

gives this error:
"type 'class CarClass' argument given to 'delete', expected pointer"

Also the CarClass is a simple class that owns a constructor and a deconstructor, so I dotn quite understand why I get such a error.

I am pushing the cars in with this comand:
1
2
3
4
5
6
7
CarClass* SimulatorClass::AddCar()
{
    CarClass* newCar = new CarClass();
    cars.push_back(*newCar);
    //newCar->id = SomeIdCounter();
    return newCar;
}


Any ideas?
comparison between signed and unsigned integer expressions [-Wsign-compare]


That's because of this:
for (int i = 0; i <= cars.size(); i++)

i is an int, and size() returns a size_t i believe (well, a size_type, which is an unsigned int). You could cast the return of size() to be an int.

How have you defined 'cars'?

Ah. Messages and code imply you do NOT have a vector of pointers. Here,
cars.push_back(*newCar);
you are pushing a dereferenced VALUE onto your vector. which also explains why it makes no sense to call delete on the elements.
Last edited on
Hello, thanks, I solved the loop issue now. But as for the error.
I'm defining cars like this:

1
2
3
4
5
6
class SimulatorClass
{
    public:
        vector<CarClass> cars;
        CarClass* AddCar();
};


How can I solve the pushing issue ?
You don't have a pushing issue. You can just push in an object, and not a pointer to one.
Then you have no need to free up any memory.

OR

Do you actually want a vector of car pointers? In that case rather than
1
2
CarClass* newCar = new CarClass();
    cars.push_back(*newCar);


you need something like:

1
2
CarClass* newCar = new CarClass();
    cars.push_back(newCar);
Last edited on
Tryed it and this is what I get:
"error: no matching function for call to 'std::vector<CarClass>::push_back(CarClass*&)'"
Tryed it


What did you try? Post your updated code.

And tell me do you want a vector of Car pointers or a vector of Car objects?
in .h
1
2
3
4
5
6
7
8
class SimulatorClass
{
    public:
        vector<CarClass> cars;
        CarClass* AddCar();
        void UpdateSimulation(float dT);
        void FreeSimulation();
};

in .cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
CarClass* SimulatorClass::AddCar()
{
    CarClass* newCar = new CarClass();
    cars.push_back(newCar);
    return newCar;
}
void SimulatorClass::UpdateSimulation(float dT)
{
        // activatig this does nothing, might be the same issue as deleting...
        for (std::size_t i = 0, max = cars.size(); i != max; ++i)
        {
            cars.at(i).Update(dT);
        }
}
void SimulatorClass::FreeSimulation()
{
    //not functional (returns a error while compiling)
    if (!cars.empty())
    {
    
        for (std::size_t i = 0, max = cars.size(); i != max; ++i)
        {
            delete( cars.at(i) );
            cars.at(i) = NULL;
        }
    }
    cars.clear();
}


this is the updated code
Last edited on
You appear to have completely ignored mytexe's (very important) question:

And tell me do you want a vector of Car pointers or a vector of Car objects?
Sorry MikeBoy.
Well I want a vector of Car Objects. Tho I'm accesing them in C# with a dll and I use Marshal.IntPtr for doing this, it works fine with the methods I have created so far.

What would you sugest? Thanks!
Well I want a vector of Car Objects.
[...]
What would you sugest?

I would suggest you read mutexe's post more carefully.

He told you:

You don't have a pushing issue. You can just push in an object, and not a pointer to one.
Then you have no need to free up any memory.

He then went on to give you an alternative if you wanted a vector of Car pointers, but you've just said you don't want that.
Well, do I need to set up my class differently? Because the comiler gives errors if I remove pointers...

It's geting a little confused on a simple topic. basically I want to but every object that I create in a vector (cars/wheels) and later call a single method to remove them all. Currently I have to use pointers, because otherwise I get errors...

The last error when I removed all pointers
"error: conversion from 'CarClass*' to non-scalar type 'CarClass' requested"
Well, do I need to set up my class differently? Because the comiler gives errors if I remove pointers...

What error? Where? What do you mean by "remove pointers"? We can't help you if you give us cryptic snippets of information. If you want us to help, you need to be specific and precise. We can't read your mind to determine what you mean by vague comments.

It's geting a little confused on a simple topic. basically I want to but every object that I create in a vector (cars/wheels) and later call a single method to remove them all.

If you read the documentation for std::vector, you'll see that it already has a method for clearing all its contents.

The last error when I removed all pointers
"error: conversion from 'CarClass*' to non-scalar type 'CarClass' requested"

On what line of code? What do you mean by "removed all pointers"? You must have been trying to use a pointer to CarClass somewhere.
Last edited on
I'm sorry my english is not the best. Anyway I finnaly figured out what I had to fix.
from: vector<CarClass> cars; to this: vector<CarClass*> cars;

And as for the vector cleaning. It was just an example of what I want to do with all of the objects that I store in a vector. A better example would be a "global" update that will update all the created car's with a single method. I got it working now.

Thanks for your time and help.
You need to understand that c++ is not c#
To create an object with the default constructor you'll do CarClass car;
To create an object using another constructor CarClass car("Mini");

So you can simply do
1
2
std::vector<CarClass> car;
car.push_back( CarClass("fiat 600") );
when the vector dies, all its elements are eliminated. You do not need to write a destructor in this case (because of the rule of three, you don't need to write an assignment operator or a copy constructor either)


If you want to use polymorphism, then you need to store pointers.
1
2
std::vector<CarClass *> cars;
cars.push_back( new sedan("chevrolet") ); //note: sedan is a type of car (class sedan: public CarClass) 
when the dies, its elements die with it. Note however, that its elements are pointers; the pointers are eliminated, not what they pointed to.
So you need to create a destructor
1
2
for( size_t K=0; K<cars.size(); ++K)
   delete cars[K];
Also, consider what happens if you do
1
2
std::vector <CarClass*> a_parking_lot;
a_parking_lot = cars;


Instead of codifying the destructor, copy-constructor and assignment operator yourself, you may store smart pointers (that will delete the pointed object correspondingly)
Last edited on
Topic archived. No new replies allowed.