PocketMatrix forums

A read-only archive of the PocketMatrix forums (2001–2014). Posting and accounts are closed.

Referencing members of the MyGame class from a separate CPP?

Can anyone tell me how to access the MyGame class variables etc from a separate .cpp file.

I can declare a prototype for a function within the MyGame class and so long as the function is placed in the same .cpp file outwith the class everything builds fine but when I move the function body into a separate .cpp which includes
using namespace Frog; and
#include <PocketFrog.h>
I get undeclared identifiers etc etc

None of my C++ books nor 2 hours searching web C++ resources tells me how to do this but it's obviously required to write a big program as I can't have everything on one source nor do I want to have to pass values as function parameters all the time.

In C you just have function prototypes and includes & everything works but there's something I'm missing with C++.
Declare your class prototype in separate .h file. Inlude pocketfrog.h there.
Then include your .h in all .cpp where you placing class functions bodys.

Something like that:
Code:

main.h:
#include <PocketFrog.h>
using namespace Frog;
class MyGame : public Game
{
public:
QuadLines();
virtual bool GameInit();
virtual void GameEnd();
virtual void GameLoop();
virtual void SplashScreen();
};

a.cpp:
#include "menu.h"
void MyGame::SplashScreen()
{
}

b.cpp:
#include "menu.h"
void MyGame::GameInit()
{
}
That was me. I've made some errors in code. Cant edit the post.
Edited piece of code:
Code:

main.h:
#include <PocketFrog.h>
using namespace Frog;
class MyGame : public Game
{
public:
MyGame();
virtual bool GameInit();
virtual void GameEnd();
virtual void GameLoop();
virtual void SplashScreen();
};

a.cpp:
#include "main.h"
void MyGame::SplashScreen()
{
}

b.cpp:
#include "main.h"
void MyGame::GameInit()
{
}


Is that what you looked for?
Everything that works under C will also work under C++... Don't try to jump steps while learning classes... But my guess is that you were looking for what Mr. Prayer posted.

You can also take a look at PF's source code... There is plenty of classes there... Perhaps starting with "graphicbuffer.h" / "graphicbuffer.cpp" as an example.
Conan · 25 Sep 2002, 17:57 UTC — Thanks for the help Mr Prayer & Kzinti
Thanks for the code snippet. That's pretty much what I was beginning to think. I will try this out.

Re: classes I'm not really trying to go totally OO and I don't want to have to re-write working code. The main aim is to have the ability to modularise the sources as Space Treker is a pretty big bit of code with around 10 separate sources at present.

I will have a look at the PocketFrog sources to see how they deal with this.
What I meant to say is that if your C code is working, there is no need to convert it to classes. Just leave it alone and it will work.
Conan · 25 Sep 2002, 18:49 UTC — Great, that works fine !!!!
My 'mygame.h' works fine so I can now start adding in my additional source files and give them access to the member variables etc from MyGame.

( I had a day's leave today so I decided to get most of the setup work out of the way for the big push ).

Many thanks My Prayer + Kzinti
Mr.Prayer · 25 Sep 2002, 18:58 UTC — Great, that works fine !!!!
Conan wrote:
Many thanks My Prayer + Kzinti

I'm not Your Prayer.
Sorry... 8)
Conan · 25 Sep 2002, 19:34 UTC — Sorry Mr Prayer
And they (r y) are not even together on the keyboard, well I have been typing all day and my eyes are sore.

Happy coding