Where to define #include and and the Pokitto::Core game statement?

Hello,
I have two files, one is the main.cpp and one a Game.cpp that I want to include with a header in main.cpp. In need the Pokitto::Core game in both .cpp files.

// main.cpp
#define DISABLEAVRMIN

#include "Pokitto.h"
#include "Game.h"
#include <string>

Pokitto::Core game;

using namespace std;
// Game.h
#include <string>
#include "time.h"
#include "Pokitto.h"

using namespace std;
// Game.cpp
#include "Game.h"

Pokitto::Core game;

It seems that I need to add some #include statements only once, but I don’t know where.
Thanks in advance! :slight_smile:

1 Like

In Game.cpp or Game.h, mark Pokitto::Core game as extern, i.e.

extern Pokitto::Core game;

(There’s a decent explanation of this use of extern here.)

Though truthfully you don’t need to make an instance/variable, you can just use Pokitto::Core on its own because all the functions are static.
(Like I did in Noughts And Crosses.)


By the way, using namespace std; is usually considered bad practice for two reasons.

  • When it’s used in a header file it leaks out and affects all the code that comes after it
  • If two different namespaces use the same name for something and they’re both included, it can cause ambiguity that the compiler can’t resolve (as explained here and here)

The second problem is probably slightly less likely to happen in the Pokitto’s environment,
but the first one is still an issue.
The first issue doesn’t affect .cpp files so it’s fine to put them there, but try to avoid doing it in .h files.

2 Likes

[quote=“Pharap, post:2, topic:1052”]Though truthfully you don’t need to make an instance/variable, you can just use Pokitto::Core on its own because all the functions are static.[/quote]And how do I need to include Pokitto::Core to access game? :slight_smile:

If you want to use Pokitto::Core without making a variable for it, you just replace where you’ve been using game. with Pokitto::Core::, e.g.

Pokitto::Core::begin();

while (Pokitto::Core::isRunning())
{
	if (Pokitto::Core::update())
	{
		this->update();
		this->draw();
	}
}

And you only need to include Pokitto.h to access it, you don’t need to do any extra declarations.

1 Like

Thanks! ^^

1 Like