c++ - '(' 标记 ~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++。请帮我!!
解决方案
类声明中有几个特殊函数必须与类同名:
- 构造函数(默认,带参数等)
- 析构函数
- 复制构造函数
- 移动构造函数
在您的类Game
中,构造函数和析构函数的名称是GameC
. 只需将其名称更改为Game
以修复错误。
推荐阅读
- javascript - 使用 PIXI.js 绘制千条线
- javascript - Bootstrap Modal,在按钮单击时调用 Action Result 方法
- r - 在R中的整洁数据中查找多个组的平均值
- ios - 为什么 UICollectionView 中的单元格被截断?
- angular - Angular 4+ Firebase Visual Sutudio Online Azure CI 构建错误
- c# - 转换时输入字符串的格式不正确,将值插入循环内的数据库
- sql - SQL Server Management Studio 需要完整的表路径
- django - DjangoForm 类元覆盖
- excel - 如何明智地使用高级过滤行
- excel - 想要一个包含非连续单元格的命名列表