Archived

This topic is now archived and is closed to further replies.

neurokaotix

I want to make an class inheritance public but I dont know how

Recommended Posts

CSprite Sprite; Sprite.Initialize("crate2.jpg"); I use the above code to inherit the functionality of the class CSprite to something I created called Sprite. I''m using Sprite.Render(); in my RenderAll() function where everything in my game is rendered. When I call Sprite.Render() in RenderAll() I get errored out and my debug log says: DXMain.cpp(219) : error C2065: ''Sprite'' : undeclared identifier DXMain.cpp(219) : error C2228: left of ''.Render'' must have class/struct/union type egads! Now, do I have to somehow make Sprite public or global? If so, how do I do that? I''m totally newb lol

Share this post


Link to post
Share on other sites
you need to pass it as a parameter (either as a reference or as a const reference) to whatever functions are using it.

---
Come to #directxdev IRC channel on AfterNET

Share this post


Link to post
Share on other sites
quote:

CSprite Sprite;
Sprite.Initialize("crate2.jpg");



in which file and function is this present?

quote:

Sprite.Render();

in my RenderAll() function where everything in my game is rendered. When I call Sprite.Render() in RenderAll() I get errored out and my debug log says:



in which file is this present?
or better, try posting the code.

are you aware of local variables and objects concept???
i think you are declaring a local variable (CSprite Sprite) in some function and trying to use the same variable in another function (RenderAll()). This is not possible cuz the variable Sprite cease to exist after the first function returns. Also if the RenderAll() is called from the first function, then Sprite object exist but the RenderAll function cannot see it since Sprite is a local variable to the first function.

to solve the problem (this is not the correct way though!)

  
void main()
{
CSprite Sprite;
Sprite.Initialize("crate2.jpg");
// some code

...
RenderAll(&Sprite);
// Some more code

...
}

void RenderAll(CSprite *sprite)
{
// some code

...
sprite->Render();
// some more code

...
}


You should ideally maintain a linked list (or dynamic array) of all the sprites created. When ever you create a sprite object (create it dynamically using new) add this object to the linked list. Then the RenderAll function can accept this linked list as the parameter and traverse through this linked list and call sprite->Render() for each object in the list.

either way, you need to organize the code in a better way...

Share this post


Link to post
Share on other sites