How to delete vector class objects..

Hi! First of all I want to wish you a Happy new year! :)
First of all you need to know that i am new to C++, and i apologize for the things you are going to see... :P I am working on a project and i have some runtime errors..

I have a class,named Fish.
and i create new objects of this class in the vector.

--->
1
2
std::vector<Fish*> f;
f.push_back(new Fish);


I think so far is ok.. But it shows a runtime error, after i delete
many objects:

---->
1
2
3
delete f.at(c);
     f.at(c)=NULL;
     f.erase(f.begin()+c);


Any ideas??

I would like to know, why you are using pointers and dynamic memory? This is already an over complication for such a simple task...
We want some fish to eat other fish.. Do you have any suggestions??
Yes; don't use pointers. You will find your life much easier if you only use pointers when they are more convenient. In this case they are less convenient, so don't use them. ;)
The way to delete the pointer object is

1
2
3
4
5
6
std::vector<Fish*> f;
f.push_back(new Fish);

for( int i = 0 ; i < f.size(); i++)
   delete f[i];
   f.clear();
Last edited on
Bluecoder, what if they want to only remove a specific fish from the vector? The task becomes much simpler without pointers because you don't have to worry about deleteing what they refer to.
Yes.. I want to delete only a spesific object in my class..
But using destructor..
Can it work without pointers?
The project requires the use of destructors..
If you have a vector of Fish like std::vector<Fish>, the destructors of objects will be automatically called when you erase them from the vector so you don't need pointers.
The project requires the use of destructors..


You don't have to write them. The compiler makes them for you if you don't.
i'll try it right away and i'll inform u.. thanks :)
So... I have another problem now.. i replaced all -> with . and i have douzens of errors now..
what do i have to change to work properly?
what do i have to change to work properly?

You have to fix the mistake you made.

Without showing us bad code, that's the best we can suggest.
All i ask for is an example......
What can i do? "Erase" * symbol?
1
2
std::vector<Fish> f;
f.push_back(new Fish);


and then how can i insert,size of the object..
f.at(i)->fishSize();

Please help me...
Last edited on
1
2
3
std::vector<Fish> f;
Fish aFishObject;
f.push_back(aFishObject);


At this point, f[0] is an object of type Fish.

If Fish looked like this:

1
2
3
4
5
struct Fish()
{
  void someInternalFunction();
  int  someInternalVariable;
}


you could access that function and variable like this:

1
2
f[0].someInternalFunction();
f[0].someInternalVariable;


Stop trying to use new. You don't need it here. It makes you look like you're trying to force Java into C++ syntax.
Last edited on
OMG!!! Thank You soooo much guys!!! Problem solved!
Last edited on
I have my doubts that the problem is really solved.
While using pointers might have been a bad idea, the code you used was correct.
So the actual problem is most likely still present, just hidden for the moment.
thanks L B , I should have thought about that .
Topic archived. No new replies allowed.