C++编译器问题
给定以下两个头文件:
#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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
你有一个循环依赖。
当
window_handler.h
包含event_handler.h
时,您已经定义了WINDOW_HANDLER_H
但实际上尚未到达定义该类的位置。当event_handler.h
尝试包含window_handler.h
时,它不会因为WINDOW_HANDLER_H
如前所述,您需要在
中转发声明event_handler.h
通过删除window_handler.h
的包含并将其替换为:You have a circular dependency.
When
window_handler.h
includesevent_handler.h
you've definedWINDOW_HANDLER_H
but haven't actually reached the point where the class is defined. Whenevent_handler.h
tries to includewindow_handler.h
it doesn't because ofWINDOW_HANDLER_H
As noted, you need to forward declare in
event_handler.h
by removing the include forwindow_handler.h
and replacing it with:在 event_handler.h 中,删除该行
并将其替换为
这里的问题是您的包含列表中有一个循环。因此,由于包含保护,您将拥有一个尝试使用未定义的 WindowHandler 或未定义的 EventHandler 的文件。看一下预处理器的输出,这应该更有意义。
In event_handler.h, remove the line
and replace it with
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.