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
Class Function Problem
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.
map is local to the main() function.
Either make map global or pass a pointer to it to the game class
Either make map global or pass a pointer to it to the game class
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)
Is there a way for a class to pass a pointer to itself as a function parameter?
For example:
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
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
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.
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! =)
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! =)
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?
quote:Original post by Chryzmo
Do you mean to store the map itself in CGame
Yes. IE
class CGame{/* Stuff */private: CMap map_m;};
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement