Virtual function help!

Started by
5 comments, last by BradDaBug 22 years, 4 months ago
I''m trying to write a GUI. Each GUI object, like buttons, windows, whatever, are specific classes that are all derived from a base GUIObject class. I keep track of these object with a linked list full of pointers to the class, but they''re all typecast as just pointers to the base class the classes are all derived from. The base class has a virtual function Draw(). as well as all the other classes. I want it to go through the list and draw all the objects. Heres some code: while(tempnode != NULL) { tempnode->ptr->Draw(); tempnode = tempnode->next; } This is the meat of the transversal. tempnode is the current node we''re working with, ptr is a pointer to the base class which should be the same thing (should point to the same thing) as the main class for whatever particular type of object we''re working with, and Draw() is the virtual function in question. The problem, I get a nice crash when it tries to call this Draw() function. What''s going on?
I like the DARK layout!
Advertisement
That''s not enough code to really help. Are you 100% sure that ptr points to a valid variable in memory?

[Resist Windows XP''s Invasive Production Activation Technology!]
Yes, make sure your linked list code is impeccable. Better yet, use the STL''s class list (that''s how I did my GUI).

Chris Barry

Jesus saves ... the rest of you take 2d4 fire damage.

Ok, I''ve done some debuggin, and I''ve discovered for some reason tempnode->ptr is NULL in the drawing function.

The strange thing is, I''m using almost the same exact code in FindControl() as I am the code to draw everything. The only difference is FindControl() does a strcmp() with tempnode->ptr->Name and the drawing function does tempnode->ptr->Draw().

FindControl() works perfectly! I don''t understand why the drawing function is screwing up!
I like the DARK layout!
Holy flaming animals, I think I have figured it out! How could I have been so stupid!

I was creating the controls in the function InitGame()! The drawing function was being called in GameMain()! The controls were going out of scope!

AAAAAAAAHHHHHHH!!!!!! Why is it always something so stupid?
I like the DARK layout!
Wait a minute, its still not working. I created created some functions that did malloc() to create the controls.

The controls exist and all, but its still crashing at the controls Draw() function!

I wrote a smaller console app to figure out what is going on, and its doing the same thing. Heres the class definitions:

class BaseClass
{
public:
virtual void WriteText(void);
};

class Lego : public BaseClass
{
public:
virtual void WriteText(void);
};

class Toy : public BaseClass
{
public:
virtual void WriteText(void);
};

Heres the main code:

include
#include "main.h"


BaseClass* base;
Lego* legos;
Toy* toys;


main()
{

printf("Calling function...\n");

base->WriteText();
legos->WriteText();
toys->WriteText();


return(NULL);
}

void BaseClass::WriteText(void)
{

printf("Called BASECLASS! \n");
}

void Lego::WriteText(void)
{
printf("Called Lego!\n");
}

void Toy::WriteText(void)
{
printf("called toys!\n");
}

It crashes when it tries to call WriteText(). Why?

I like the DARK layout!
Because you never actually created an object that each of the pointers refers to. Add this to your code.

  base  = new BaseClass;legos = new Lego;toys  = new Toy;  

This topic is closed to new replies.

Advertisement