I'm starting programming with raylib and tried to make a snake game. And I wanted to put everything that happens in a Game class. So i included the header file of my snake class and then tried to create an objekt in my game class but i am then getting the error : unknown override specifier.
#ifndef GAME_H
#define GAME_H
#include "food.h"
#include "snake.h"
class Game {
public:
void Draw();
void Update();
void MoveSnake();
private:
Snake snake = Snake(); // error here
Food food = Food(); // error here
};
#endif // GAME_H
The Problem is the same for both classes. Exaple for class snake:
#ifndef SNAKE_H
#define SNAKE_H
#include "header.h"
#define COLOR_R 43
#define COLOR_G 51
#define COLOR_B 24
#define COLOR_A 255
class Snake {
public:
int i;
void Draw();
void Update();
void Move();
private:
std::deque<Vector2> body = { Vector2{6, 9}, Vector2{5, 9}, Vector2{4, 9} };
Vector2 direction{ 1, 0 };
int j;
};
#endif
I know that I dont get the error if i implement the class in the same file, but I want for future project and for good practise making it in a seperate file. Everything I found until now was nested classes but people are there just writing the class directly in another. Hope you can help me (Sorry for my bad english).
Rest of the files:
#include "header.h"
Color green = Color{ 173, 204, 96, 255 };
double lastUpdateTime{};
bool eventTriggered(double interval) {
double currentTime = GetTime();
if (currentTime - lastUpdateTime >= interval) {
lastUpdateTime = currentTime;
return true;
}
return false;
}
int main() {
InitWindow(cellSize * cellCount, cellSize * cellCount, "Snake");
SetTargetFPS(60);
Game game = Game();
while (!WindowShouldClose()) {
BeginDrawing();
if (eventTriggered(0.2)) {
game.Update();
}
game.MoveSnake();
// Drawing
ClearBackground(green);
game.Draw();
EndDrawing();
}
CloseWindow();
return 0;
}
Game cpp:
#include "game.h"
#include "snake.h"
void Game::Draw(void) {
this->snake.Draw();
this->food.Draw();
}
void Game::Update(void) {
this->snake.Update();
}
void Game::MoveSnake(void) {
this->snake.Move();
}
snake cpp:
#include "snake.h"
Snake::Snake() {};
void Snake::Draw(void) {
for (size_t i{}; i < body.size(); ++i) {
Rectangle segment = Rectangle{ this->body[i].x * cellSize, this->body[i].y * cellSize, cellSize, cellSize };
DrawRectangleRounded(segment, 0.5f, 6, { COLOR_A, COLOR_G, COLOR_B, COLOR_A });
}
}
void Snake::Update(void) {
this->body.pop_back();
this->body.push_front(Vector2Add(this->body[0], this->direction));
}
void Snake::Move(void) {
if (IsKeyPressed(KEY_UP) && this->direction.y != 1) {
this->direction = Vector2{ 0, -1 };
}
if (IsKeyPressed(KEY_DOWN) && this->direction.y != -1) {
this->direction = Vector2{ 0, 1 };
}
if (IsKeyPressed(KEY_LEFT) && this->direction.x != 1) {
this->direction = Vector2{ -1, 0 };
}
if (IsKeyPressed(KEY_RIGHT) && this->direction.x != -1) {
this->direction = Vector2{ 1, 0 };
}
}
the foot you can just take it out of the code its not neseccary header:
#ifndef HEADER_H
#define HEADER_H
#include <iostream>
#include <raylib.h>
#include <deque>
#include <raymath.h>
#include "game.h"
constexpr int cellSize{ 30 };
constexpr int cellCount{ 25 };
#endif
The issue could be that there is no Snake() function declared anywhere. You should create a constructor for each class, and declaring any instances of that type using the constructor.
Example:
Then declaring an object of type Snake like this
Hope this helps.