开发者

C++ Compiler Issues

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 so开发者_运维技巧mething here?


You have a circular dependency.

When window_handler.h includes event_handler.h you've defined WINDOW_HANDLER_H but haven't actually reached the point where the class is defined. When event_handler.h tries to include window_handler.h it doesn't because of WINDOW_HANDLER_H

As noted, you need to forward declare in event_handler.h by removing the include for window_handler.h and replacing it with:

class WindowHandler;


In event_handler.h, remove the line

#include "window_handler.h"

and replace it with

class WindowHandler;

The issue here is that you have a cycle in your include lists. So because of the include guards, you will either have a file that tries to use an undefined WindowHandler, or an undefined EventHandler. Take a look at the preprocessor output and this should make more sense.


Your headers have a circular dependency of includes. Depending on your needs you might be able to change one to a forward declaration, or you'll have to create a third header with the required common code in it.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜