Given the following two header files:
#ifndef EVENT_HANDLER_H
#define EVENT_HANDLER_H
#include <SFML/Window.hpp>
#include <SFML/Window/Event.hpp>
#include "window_handler.h"
class EventHandler
{
public:
EventHandler(WindowHandler & classOwner);
WindowHandler * m_windowHandler;
private:
bool m_leftKeyDown;
bool m_rightKeyDown;
bool m_upKeyDown;
bool m_downKeyDown;
unsigned int m_mouseX;
unsigned int m_mouseY;
};
#endif
AND
#ifndef WINDOW_HANDLER_H
#define WINDOW_HANDLER_H
#include <SFML/System.hpp>
#include <SFML/Window.hpp>
#include "event_handler.h"
class WindowHandler
{
public:
WindowHandler();
sf::Window m_app;
private:
EventHandler m_eventHandler;
};
#endif
I get the following output:
In file included from window_handler.h:6:0,
from main.cpp:3:
event_handler.h:13:29: error: expected ‘)’ before ‘&’ token
event_handler.h:15:2: error: ‘WindowHandler’ does not name a type
As far as I know, though, I’m doing everything perfectly fine. Am I missing something here?
You have a circular dependency.
When
window_handler.hincludesevent_handler.hyou’ve definedWINDOW_HANDLER_Hbut haven’t actually reached the point where the class is defined. Whenevent_handler.htries to includewindow_handler.hit doesn’t because ofWINDOW_HANDLER_HAs noted, you need to forward declare in
event_handler.hby removing the include forwindow_handler.hand replacing it with: