I want a global variable (object) that I can easily access whenever and wherever I wish. The problem is that my source code has multiple *.hpp and *.cpp files that share the same variable! I've searched and found similar questions here but somehow they didn't solve my problem.
- With keyword extern there are undefined reference errors
- Without keyword extern there are multiple definition errors
- With keyword extern and re-declarations in all *.cpp files *.cpp file without keyword extern there are multiple definition errors
- With keyword extern and re-declaration in only one *.cpp file without keyword extern the variables work fine only in that *.cpp file
GameStateManager.hpp
/// GLOBAL VARIABLES
extern sf::RenderWindow window;
extern sf::Event event;
/// GAME STATES
#include "LogoState.hpp"
class GameStateManager
{
...blah blah blah...
};
GameStateManager.cpp
GameStateManager::GameStateManager()
{
window.create(sf::VideoMode(WIDTH, HEIGHT), TITLE, FLAGS);
window.setFramerateLimit(FPS);
gamestatescontainer.emplace_back(new LogoState);
}
LogoState.cpp
ListOfGameStates LogoState::run()
{
while (window.isOpen())
{
window.waitEvent(event);
if (event.type == sf::Event::Closed) window.close();
}
return ListOfGameStates::STATE_EXIT;
}
Please, help me!