Archived

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

Chryzmo

Class Function Problem

Recommended Posts

Chryzmo    122
Heyas, I currently have my project seperated in two classes. CGame and CMap. I need a function in CGame to be able to call a function in CMap(both are public members). I am not quite sure how to get this to work.

int main
{
CGame game;
CMap map;

//and so on...

}

CGame::GetInput()
{
// This is the definition of the GetInput() function from CGame(public)

//...

    if (cBoard[mYPOS][mXPOS] == ''U'')
    {
        iCurrentLevel++;
        if (iCurrentLevel == 10)
        {
            map.LoadFile(); // Here is where I need to call a function from CMap

            iCurrentLevel = 0;
            map.LoadMap(iCurrentLevel); // Here as well

        } else {
            map.LoadMap(iCurrentLevel); // And finally here

        }
    }
//...

}

The LoadMap function is a public member of CMap.

There error(in Dev-C++) is: TAGFunctions.cpp: In member function `void CGame::GetInput()'': TAGFunctions.cpp:90: `map'' undeclared (first use this function) TAGFunctions.cpp:90: (Each undeclared identifier is reported only once for each function it appears in.) I think the problem is that when the code is compiled the function declarations are compiled before main.cpp and so the compiler cannot find the LoadMap function. I am not sure how to tell it to point to my CMap class. Maybe someone else can help? Thanks, Chris

Share this post


Link to post
Share on other sites
Guest Anonymous Poster   
Guest Anonymous Poster

The CMap you declare in main() is only in the scope of main(). It cannot be accessed outside of its scope. You have to pass an instance of CMap to CGame, otherwise what is it suppose to operate on?

Do something like this:

CGame::GetInput(CMap& map)

Share this post


Link to post
Share on other sites
Chryzmo    122
Is there a way for a class to pass a pointer to itself as a function parameter?

For example:



void CGame::LaunchGame()
{
CPlayer player; // Create an instance of CPlayer

CMap map; // Create an instance of CMap


map.LoadFile();
map.LoadMap();

while (gbGame == true)
{
map.Display();
map.GetInput(game); //This function needs to access a method of CGame

}
}


Trying what AP said I get this error:

TAGGame.cpp: In member function `void CGame::LaunchGame()'':
TAGGame.cpp:28: `game'' undeclared (first use this function)

Thanks,
Chris

Share this post


Link to post
Share on other sites
quote:
Original post by Chryzmo
Is there a way for a class to pass a pointer to itself as a function parameter?


use the keyword "this"

also, another alternative would be to encapsulate a map inside game. Logically, in most games, the game "has a map" loaded.


Share this post


Link to post
Share on other sites
VThornheart    246
If the cGame object needs a pointer to itself, as it sounds like you''re talking about most recently, it automatically has one.

Using the keyword "this" in an object''s method is the same as using a pointer to the individual object instance. For example:

this->cmap.loadmap() will run the loadmap function for the cmap of whatever object calls the function containing it.

Hope this helps! =)

Share this post


Link to post
Share on other sites
Chryzmo    122
quote:
Original post by Polymorphic OOP

also, another alternative would be to encapsulate a map inside game. Logically, in most games, the game "has a map" loaded.





Thanks for the reply.

Do you mean to store the map itself in CGame or just junk the whole CMap class and put all those functions and variables into CGame?

Share this post


Link to post
Share on other sites