SDL和xcode5 - SDL在不同的类

SDL and xcode5 - SDL in different classes

本文关键字:SDL xcode5      更新时间:2023-10-16

我遇到了以下问题:

我得到了我的主c++文件,其中包括

#include <SDL2/SDL.h>

在写了一些SDL c++代码之后,我想把我的程序分成不同的类。

问题是我试着说:

#include <SDL2/SDL.h>

在新的"引擎"类中,但它似乎不包括SDL。

我使用xcode 5。如果我在main.cpp

中编写代码,SDL框架就可以正常工作。

引擎类:

#include "Engine.h"
#include <SDL2/SDL.h>
using namespace std;
class Engine
{
    SDL_Window *window = NULL;
    SDL_Surface *screenSurface = NULL;
public:
    Engine();
    bool init();
    bool loadMedia();
    void close();
}
我还在考虑我需要什么样的课程。这是Engine.h

#ifndef __Engine4__Engine__
#define __Engine4__Engine__
#include <iostream>
#include <SDL2/SDL.h>
class Engine
{

}
#endif /* defined(__Engine4__Engine__) */

当我写SDL_

时,我的xcode5没有提供建议。

问题是您没有创建Engine.h,并且在它不存在时试图引用它。问题不在于SDL,而在于您不确定如何在c++中创建class

需要同时创建Engine.hEngine.cpp

Engine.h看起来像

#ifndef ENGINE_H
#define ENGINE_H
#include <SDL2/SDL.h>
class Engine
{
    public:
        Engine();
        bool init();
        bool loadMedia();
        void close();
    private:
         SDL_Window *window;
         SDL_Surface *screenSurface;
};
#endif

然后你需要创建一个Engine.cpp文件,看起来像

#include "Engine.h"
Engine::Engine() : 
     window(nullptr),
     screenSurface(nullptr)
{
}
// Rest of code from header file