Hello, I'm trying to call a function from a class that has inherited from a base class. I've seen this been done, but unfortunately I get a crash when running the program.
Hello, I'm trying to call a function from a class that has inherited from a base class. I've seen this been done, but unfortunately I get a crash when running the program.
The error is caused by Enemy recursively calling its own Render() function.
From the look of the classes, you are trying to get enemies to call the render of mrBogey through the instances variable. This does not work because both objects create their own instances variable.
Thanks for the reply. I've managed to fix the initial crash by declaring the instances vector as a global variable. However, the program now crashes with a stack overflow.
It's still exactly the same problem - Enemy::Render() is calling itself recursively, an infinite number of times.
Why is Enemy::Render() calling the Render() method on multiple Enemy objects (including itself)?
It looks to me as though you might be confused about what you want the Enemy class to represent. Is it a single enemy, or is it a collection of enemies?
virtualbool Render()
{
for (std::vector<Enemy*>::iterator i = instances.begin(); i != instances.end(); ++i)
{
if((*i) != this)
(*i)->Render();
}
returntrue;
}
It would be a collection of Enemies. This is actually some playing around I'm doing, most likely going to use this in a rendering engine of some sort -- Have a base rendering class which will include all the required things such as render, pre-render etc which things like a lighting class can inherit from and automatically be added to the game loop through this method.
I think your modified code will still have the same problem. When more than one enemy is the list, Enemy 1 will call Render() on Enemy 2, which will call Render() on Enemy 1, which will call Render() on Enemy 2, etc.
You need to think more clearly about the design. Separate out the idea of an "enemy" from the idea of "a collection of enemies". Have different classes to represent those different concepts. Then the collection class can iterate over the individual enemies, calling Render() on each one in turn.
As for the design of the class, it isn't finished. This code was more of a proof of concept, to prove that I can get this code to run fine so I can implement it for its actual use later.
The final class would look somewhat like this: (This is for a rendering engine)
3. 2 inherited classes and the base class. I'm not really sure how to make it so it doesn't add the base class to the vector, the if((*i) != this) prevents it from causing a problem but if you know a better way of getting round this please let me know.
You could split it up so that you have things that can be rendered. The base class could be called Renderable, or RenderableObject or sometihng like that.
And then you could have a Renderer class that handles the Renderable objects and makes sure they are being rendered correctly.