首页 > 解决方案 > '(' 标记 ~GameC() 之前的预期类名

问题描述

我还在学习 c++ 和 SDL2

我正在尝试编译此代码,但遇到了一些
麻烦。

麻烦: 当我尝试编译我的代码时,我得到了这个错误:

'(' 标记 ~GameC() 之前的预期类名

我试图了解构造函数和析构函数
这是我的代码

游戏.hpp


#pragma once

#include <SDL2/SDL.h>
#include <SDL2/SDL_image.h>
#include <iostream>


class Game
{
public:

    GameC();
    ~GameC();
    SDL_Renderer *render;
    SDL_Window *window;
    
    SDL_Event event;


    bool running = true;

    static void Draw(SDL_Renderer *render, SDL_Texture *texture, SDL_Rect rSR, SDL_Rect rDr){}
};

游戏.cpp


#include "game.hpp"
#include <iostream>

Game::GameC(){

    if (SDL_Init(SDL_INIT_EVERYTHING) < 0){
        std::cout << "SDLFailed to init() :- " << SDL_GetError() << std::endl;
    }

    window = SDL_CreateWindow("Tgame", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, 800, 800, SDL_WINDOW_SHOWN);
    render = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED);
    
    while (running){
        while (SDL_PollEvent(&event)){
            if (event.type == SDL_QUIT){
                running = false;
            }
        }       

        SDL_RenderClear(render);

        SDL_RenderPresent(render);
    }
}


Game::~GameC(){
    SDL_DestroyWindow(window);
    SDL_DestroyRenderer(render);
    SDL_Quit();
}

主文件

#include <iostream>
#include "src/game.hpp"

// libs 
#include <SDL2/SDL.h>
#include <SDL2/SDl_image.h>


int main(int argc, char * argv[]){
    
    Game game;

    return 0;
}


我还在学习c++。请帮我!!

标签: c++

解决方案


类声明中有几个特殊函数必须与类同名:

  • 构造函数(默认,带参数等)
  • 析构函数
  • 复制构造函数
  • 移动构造函数

在您的类Game中,构造函数和析构函数的名称是GameC. 只需将其名称更改为Game以修复错误。


推荐阅读