c++ 从单个源文件转换到多个文件。错误:'blank'未命名类型

c++ Transitioning from single source file to multiple files. error: 'blank' does not name a type

本文关键字:错误 类型 未命名 blank 文件 源文件 单个 转换 c++      更新时间:2023-10-16

我目前正在尝试使用SDL编写一个非常简单的2d游戏引擎,以帮助自己更轻松地进行编码。不幸的是,我犯了一个错误,从一个源文件开始,现在我的代码越来越大,我正试图将现有的代码(在拆分之前运行良好(拆分为多个头文件和源文件。我遇到的第一个主要问题是,当我在init.h中声明SCREEN_HIGHT、SCREEN_WIDTH、Window和Renderer后,试图在init.cpp中定义它们时,我会得到错误:"blank"没有命名类型(blank是Window、Renderer等(。我还试图使它们全局化,这可能是问题的一部分。下面包括相关的代码(我去掉了所有我认为在这种情况下无关紧要的东西(。我怀疑我只是错过了一些非常简单的东西,但我一直无法在网上找到现有的答案,就像我以前遇到的问题一样。

main.cpp

#include "globals.h"
#include "texture.h"
....

globals.h

#ifndef GLOBALS
#define GLOBALS
#include <SDL.h>
#include <SDL_image.h>
#include <SDL_ttf.h>
#include <stdio.h>
#include <string>
#endif //GLOBALS

纹理.h

#ifndef TEXTURE
#define TEXTURE
#include "globals.h"
#include "init.h"
....
#endif // TEXTURE

纹理.cpp

#include "texture.h"
....

init.h

#ifndef INIT
#define INIT
#include "globals.h"
//screen dimensions
int SCREEN_WIDTH;
int SCREEN_HEIGHT;
//initiates SDL and creates a window and renderer
bool init();
//the created window
SDL_Window* Window;
//the renderer that will be used
SDL_Renderer* Renderer;
#endif // INIT

init.cpp

#include "init.h"
SCREEN_WIDTH = 640;
SCREEN_HEIGHT = 480;
Window = NULL;
Renderer = NULL;
bool init()
{
    bool success = true;
    if( SDL_Init( SDL_INIT_VIDEO | SDL_INIT_AUDIO) < 0)
    {
        printf( "SDL was unable to initialize! SDL Error: %sn", SDL_GetError());
        success = false;
    }
    else
    {
        if( !SDL_SetHint( SDL_HINT_RENDER_SCALE_QUALITY, "1"))
        {
            printf( "Linear texture filtering not enabled!");
        }
        else
        {
            Window = SDL_CreateWindow( "Window", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, SCREEN_WIDTH, SCREEN_HEIGHT, SDL_WINDOW_SHOWN);
            if( Window == NULL)
            {
                printf( "Window could not be created! SDL Error: %sn", SDL_GetError());
                success = false;
            }
            else
            {
                Renderer = SDL_CreateRenderer( Window, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC);
                if( Renderer == NULL)
                {
                    printf( "Renderer could not be created! SDL Error: %sn", SDL_GetError());
                    success = false;
                }
                else
                {
                    SDL_SetRenderDrawColor( Renderer, 0xFF, 0xFF, 0xFF, 0xFF);
                    int PNGImage = IMG_INIT_PNG;
                    if( !(IMG_Init( PNGImage) & PNGImage))
                    {
                        printf( "Image loading not enabled! SDL_Image Error: %sn", IMG_GetError());
                        success = false;
                    }
                    if( TTF_Init() == -1)
                    {
                        printf( "True type fonts not enabled! SDL_TTF Error: %sn", TTF_GetError());
                        success = false;
                    }
                }
            }
        }
    }
    return success;
}

在bool-init((

之前的代码顶部,我在init.cpp中得到了错误

Renderer = NULL;是一个赋值语句,不能在全局范围内使用。只能有全局范围内的声明。由于您已经在头文件中声明了Renderer,因此也不需要重新声明它。只需在一个初始化函数(例如当前的init()函数(中将其设置为null即可。