与使用类有关的两个错误

Two errors to do with using a class

本文关键字:两个 错误      更新时间:2023-10-16

我收到以下两个错误:

sdlsetup.h(15): error C2143: syntax error : missing ';' before '*'

sdlsetup.h(15): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int

这是第 15 行sdlsetup.h

CText* ctext;

该类绝对设置正确并包含在sdlsetup.h

这是为什么呢?

编辑:这是sdlsetup.h

#pragma once
#include "stdafx.h"
class SDLsetup
{
public:
    SDLsetup();
    ~SDLsetup();
    void Begin();
    void End();
    SDL_Renderer* GetRenderer();
    void EnterGameLoop();
    CText* ctext;

private:
    SDL_Window* window;
    SDL_Renderer* renderer;
    SDL_Event* mainEvent;
    int screenWidth;
    int screenHeight;
};

这是Text.h

#pragma once
#include "stdafx.h"
class CText
{
public:
    CText();
    ~CText();
    void draw(SDL_Renderer* renderer, std::string str);
    SDL_Texture* aGetMessage();
    SDL_Rect* aGetMessageRect();
private:
    TTF_Font* Sans;
    SDL_Color White;
    SDL_Surface* surfaceMessage;
    SDL_Texture* Message;
    SDL_Rect Message_rect;
};

这里的问题是编译器发出的奇怪错误消息,因为它不知道CText是什么。

您需要在使用之前声明CText

#pragma once
// Declare the CText class, so the compiler knows it exists
class CText;
class SDLsetup
{
public:
    ...
    CText* ctext;
    ...
};

您需要在 sdlsetup.h 中为 Text.h 添加包含,并在两个文件中为相关的 SDL 标头添加包含。

Text.h 需要包含 SDL.hSDL_ttf.h,sdlsetup.h 需要包含 SDL.hText.h