为什么我会收到这么多错误,为什么它告诉我窗口未定义?不会编译

时间:2013-06-14 19:53:03

标签: c++ windows

所以我正在制作一款试图使用OOP的游戏但是视觉工作室并不开心我使用VC ++ 2010 express作为我的IDE,我决定将SDL“Objectify”成可重复使用的组件,但现在我从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

2 个答案:

答案 0 :(得分:2)

你有一个循环包含。请注意WindowState.h包括Window.hWindow.h包括WindowState.h的方式?这是编译器不喜欢的永无止境的依赖循环。

如果是我的钱,我会删除#include <Window.h表单WindowState.h,然后在课程的某个地方执行所谓的前向声明:class Window;

请记住,您不能在此文件中使用此类。但是,您可以将其包含在相应的WindowState.cpp文件中并使用它。

答案 1 :(得分:0)

WindowsState.hWindowGraphics都不需要完整的定义,只需要转发声明,所以更改:

#include"Window.h"
#include"Graphics.h"

为:

class Window;
class Graphics;

Window.h中,WindowState只需要转发声明,因此请更改:

#include "WindowsState.h"

为:

class WindowState;

Graphics无法在Window.h中进行前向声明,因为必须计算该类型的完整大小才能成功编译Window

使用前向声明可以最大限度地减少文件之间的编译依赖性,并使代码编译速度更快。例如,更改Graphics.hWindow.h文件不需要重新编译仅包含WindowState.h的所有文件。