How to delete vector class objects..

Jan 4, 2012 at 1:43pm
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??

Jan 4, 2012 at 2:04pm
I would like to know, why you are using pointers and dynamic memory? This is already an over complication for such a simple task...
Jan 4, 2012 at 2:23pm
We want some fish to eat other fish.. Do you have any suggestions??
Jan 4, 2012 at 2:24pm
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. ;)
Jan 4, 2012 at 2:29pm
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 Jan 4, 2012 at 2:29pm
Jan 4, 2012 at 2:30pm
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.
Jan 4, 2012 at 2:45pm
Yes.. I want to delete only a spesific object in my class..
But using destructor..
Can it work without pointers?
Jan 4, 2012 at 2:46pm
The project requires the use of destructors..
Jan 4, 2012 at 3:08pm
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.
Jan 4, 2012 at 3:13pm
The project requires the use of destructors..


You don't have to write them. The compiler makes them for you if you don't.
Jan 4, 2012 at 3:17pm
i'll try it right away and i'll inform u.. thanks :)
Jan 4, 2012 at 3:25pm
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?
Jan 4, 2012 at 3:48pm
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.
Jan 4, 2012 at 4:23pm
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 Jan 4, 2012 at 4:23pm
Jan 4, 2012 at 4:26pm
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 Jan 4, 2012 at 4:27pm
Jan 4, 2012 at 4:42pm
OMG!!! Thank You soooo much guys!!! Problem solved!
Last edited on Jan 4, 2012 at 4:42pm
Jan 4, 2012 at 4:56pm
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.
Jan 4, 2012 at 6:38pm
thanks L B , I should have thought about that .
Topic archived. No new replies allowed.