C++编译器问题

发布于 2024-12-02 14:32:50 字数 1076 浏览 0 评论 0原文

给定以下两个头文件:

#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

并且

#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

我得到以下输出:

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

但据我所知,我做的一切都很好。我在这里错过了什么吗?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

一曲爱恨情仇 2024-12-09 14:32:50

你有一个循环依赖。

window_handler.h 包含 event_handler.h 时,您已经定义了 WINDOW_HANDLER_H 但实际上尚未到达定义该类的位置。当 event_handler.h 尝试包含 window_handler.h 时,它不会因为 WINDOW_HANDLER_H

如前所述,您需要在 中转发声明event_handler.h 通过删除 window_handler.h 的包含并将其替换为:

class WindowHandler;

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;
攒一口袋星星 2024-12-09 14:32:50

在 event_handler.h 中,删除该行

#include "window_handler.h"

并将其替换为

class WindowHandler;

这里的问题是您的包含列表中有一个循环。因此,由于包含保护,您将拥有一个尝试使用未定义的 WindowHandler 或未定义的 EventHandler 的文件。看一下预处理器的输出,这应该更有意义。

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.

濫情▎り 2024-12-09 14:32:50

您的标头具有包含的循环依赖性。根据您的需要,您也许可以将其中一个更改为前向声明,或者您必须创建第三个标头,其中包含所需的通用代码。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文