为什么我得到这么多错误,为什么它告诉我窗口未定义?不会编译
Why am I getting so many errors, why is it telling me window is undefined? wont compile
所以我正在制作一个尝试使用OOP的游戏,但visual studio不高兴我使用vc++ 2010 express作为我的IDE,我决定将SDL"具体化"为可重用的组件,但现在我从0错误到122,其中一些真的不是错误,就像它说窗口未定义,我点击去错误错误消失了…所以我遇到了一点麻烦
#ifndef GUARD_WINDOWSTATE_H
#define GUARD_WINDOWSTATE_H
#include<SDL.h>
#include"Window.h"
#include"Graphics.h"
/*
WindowSate is a component to a window class, a windowstate is a seperate
frame of a window, it could be a menu or a level or something
a windowstate handles its events through a window and draws via graphics buffer to
the window's surface it will hold the components of the window(graphics, GUI components, etc.)
and the window will hold it
*/
class WindowState {
public:
WindowState(Window* window)
:m_window(window)
{
m_window->add_state(this);
}
virtual void load() = 0;
virtual void update( SDL_Event* e) = 0;
virtual void logic() = 0;
virtual void render(Graphics g) = 0;
virtual void unload() = 0;
protected:
Window* m_window;
};
#endif
它告诉我窗口是未定义的,当它在包含的window.h中定义时这里是window.h:
#ifndef GUARD_WINDOW_H
#define GUARD_WINDOW_H
#include"SDL.h"
#include<vector>
#include<string>
#include"WindowState.h"
#include"Graphics.h"
/*The window class is the interface to the user,
it displays everything and is the top layer of the interactive application
it recieves events and passes them down the chain to states and components
it holds states and enumerates through them
*/
class Window {
private:
//width and height of the window
int m_width, m_height;
//graphics handler of the window
Graphics m_g;
//window surface
SDL_Surface* m_surface;
//event to hold all events
SDL_Event m_events;
//current and qeued states
int m_current_state;
int m_queued_state;
//Current WindowState
WindowState* m_window_state;
//is the window open?
bool m_running;
//title of the window
std::string m_title;
//vector to hold all the window states
std::vector<WindowState*> m_states;
public:
//Basic constructor
Window(int width, int height);
//constructor with title
Window(int width, int height, std::string title);
//returns the windows events
virtual SDL_Event& get_events() { return m_events; }
SDL_Surface* surface() { return m_surface; }
bool is_running() { return m_running; }
virtual void run(); //starts the window
virtual void update(); //updates the events
virtual void update_state(); //checks for a change in state
virtual void draw(); //draws current state
virtual void close(); // closes the window
void queue_window_state(int state); //sets up a window state to be activated
void add_state(WindowState* state); //adds a state to the window
void set_caption(std::string caption);
//destructor
~Window();
};
#endif
你有一个循环包含。注意到WindowState.h
包括Window.h
, Window.h
包括WindowState.h
吗?这是一个编译器不喜欢的永不结束的依赖循环。
如果这是我的钱,我会从WindowState.h
中删除#include <Window.h
,并在类之前的某个地方做所谓的前向声明:class Window;
.
请记住,你不能在这个文件中使用这个类。但是,您可以在相应的WindowState.cpp
文件中包含它并使用它。
在WindowsState.h
中,Window
和Graphics
都不需要完整的定义,只需要一个前向声明,因此更改:
#include"Window.h"
#include"Graphics.h"
:
class Window;
class Graphics;
在Window.h
中,WindowState
只需要前向声明,所以修改:
#include "WindowsState.h"
:
class WindowState;
Graphics
不能在Window.h
中前声明,因为必须计算该类型的完整大小才能成功编译Window
。
使用前向声明可以最大限度地减少文件之间的编译依赖,并使代码编译得更快。例如,更改Graphics.h
和Window.h
文件将不需要重新编译仅包含WindowState.h
的所有文件,例如
相关文章:
- 为什么输出窗口上没有显示输出?
- 为什么 string::begin() 在即时窗口中输出整个字符串
- 为什么调试时没有显示控制台窗口
- 为什么在GLFW窗口中没有用这个代码在我的屏幕上绘制立方体
- 为什么 MDI 子窗口在WM_NCCREATE后不存在?
- 为什么初始化 Wintab 扩展后没有收到WT_PACKETEXT窗口消息?
- 为什么如果我从键盘输入EOF克里昂不要在运行窗口打印程序的输出?
- 为什么抓取窗口标题的代码会导致应用崩溃?
- 为什么我的窗口不显示?
- 为什么 OpenService() 在我检查窗口服务状态时不起作用?
- 为什么在Qt中创建主窗口之前必须加载语言包?
- Qt - 为什么我无法在主窗口中为我的自定义按钮触发 mousePressEvent
- 为什么 SDL 在最后一个窗口关闭时不发出SDL_QUIT
- 为什么 random() 在窗口中不起作用
- 为什么我的窗口没有显示?(诅咒)
- 为什么错误1400无效的窗口句柄
- 为什么我的滑动窗口算法没有提供正确的答案
- 为什么窗口不登上顶部,使用setWindowpos的起源
- 为什么MessageBox在我创建的桌面中显示窗口,而AllocConsole在上一个桌面中显示它
- 为什么CWnd::CreateEx无法创建我的窗口