Class Inheritance problem

I cant figure out what I did wrong In this:
Main:
#include “raylib.h”

#include “raymath.h”

#include “Character.h”

#include “Prop.h”

#include “Enemy.h”

int main()

{

const int windowWidth{384};

const int windowHeight{384};

InitWindow(windowWidth, windowHeight, "Classy Clash");

Texture2D map = LoadTexture("nature_tileset/OpenWorldMap24x24.png");

Vector2 mapPos{0.0, 0.0};

const float mapScale{4.0f};

Character knight{windowWidth, windowHeight};

Prop props[2]{

    Prop{Vector2{600.f, 300.f}, LoadTexture("nature_tileset/Rock.png")},

    Prop{Vector2{400.f, 500.f}, LoadTexture("nature_tileset/Log.png")}

};

Enemy goblin{

    Vector2{},

    LoadTexture("characters/goblin_idle_spritesheet.png"),

    LoadTexture("characters/goblin_run_spritesheet.png")

};

SetTargetFPS(60);

while (!WindowShouldClose())

{

    BeginDrawing();

    ClearBackground(WHITE);

    mapPos = Vector2Scale(knight.getWorldPos(), -1.f);

    DrawTextureEx(map, mapPos, 0.0, mapScale, WHITE);

    for (auto prop : props)

    {

        prop.Render(knight.getWorldPos());

    }

    knight.tick(GetFrameTime());

    if (knight.getWorldPos().x < 0.f ||

        knight.getWorldPos().y < 0.f ||

        knight.getWorldPos().x + windowWidth > map.width * mapScale - 50 ||

        knight.getWorldPos().y + windowHeight > map.height * mapScale - 50)

    {

        knight.undoMovement();

    }

    for (auto prop : props)

    {

        if (CheckCollisionRecs(prop.GetCollisionRec(knight.getWorldPos()), knight.GetCollisionRec()))

        {

            knight.undoMovement();

        }

    }

   

    goblin.tick(GetFrameTime());

    EndDrawing();

}

CloseWindow();

}

CharacterBase.h:

#ifndef CHARACTER_BASE_H

#define CHARACTER_BASE_H

class CharacterBase

{

public:

CharacterBase();

private:

};

#endif

CharacterBase.cpp:
#include “CharacterBase.h”

CharacterBase::CharacterBase()

{

}

Enemy.h:
#include “raylib.h”

#include “CharacterBase.h”

class Enemy : public CharacterBase

{

public:

Enemy(Vector2 pos, Texture2D idle_texture, Texture2D run_texture);

Vector2 getWorldPos() { return worldPos; }

void tick(float deltaTime);

void undoMovement();

Rectangle GetCollisionRec();

private:

Texture2D texture{LoadTexture("characters/knight_idle_spritesheet.png")};

Texture2D Idle{LoadTexture("characters/knight_idle_spritesheet.png")};

Texture2D Run{LoadTexture("characters/knight_run_spritesheet.png")};

Vector2 screenPos{};

Vector2 worldPos{};

Vector2 worldPosOld{};

float rightLeft{1.f};

float runningTime{};

int frame{};

int maxFrames{6};

float updateTime{1.f / 12.f};

float speed{4.f};

float width{};

float height{};

float scale{4.0f};

}

Enemy.cpp:

#include “Enemy.h”

Enemy::Enemy(Vector2 pos, Texture2D idle_texture, Texture2D run_texture) :

worldPos(pos),

texture(idle_texture),

Idle(idle_texture),

Run(run_texture)

{

width = texture.width / maxFrames;

height = texture.height;

}

void Enemy::tick(float deltaTime)

{

worldPosOld = worldPos;

runningTime += deltaTime;

if (runningTime >= updateTime)

{

    frame++;

    runningTime = 0.f;

    if (frame > maxFrames)

        frame = 0;

}

Rectangle source{frame * width, 0.f, rightLeft * width, height};

Rectangle dest{screenPos.x, screenPos.y, scale * width, scale * height};

DrawTexturePro(texture, source, dest, Vector2(), 0.f, WHITE);

}

void Enemy::undoMovement()

{

worldPos = worldPosOld;

}

Rectangle Enemy::GetCollisionRec()

{

return Rectangle{

    screenPos.x,

    screenPos.y,

    width * scale,

    height *scale

};

}

Prop.h:

#include “raylib.h”

class Prop

{

public:

Prop(Vector2 pos, Texture2D tex);

void Render(Vector2 knightPos);

Rectangle GetCollisionRec(Vector2 knightPos);

private:

Texture2D texture{};

Vector2 worldPos{};

float scale{4.f};

};

Prop.cpp:

#include “Prop.h”

#include “raymath.h”

Prop::Prop(Vector2 pos, Texture2D tex) :

worldPos(pos),

texture(tex)

{

}

void Prop::Render(Vector2 knightPos)

{

Vector2 screenPos { Vector2Subtract(worldPos, knightPos) };

DrawTextureEx(texture, screenPos, 0.f, scale, WHITE);

}

Rectangle Prop::GetCollisionRec(Vector2 knightPos)

{

Vector2 screenPos { Vector2Subtract(worldPos, knightPos) };

return Rectangle{

    screenPos.x,

    screenPos.y,

    texture.width * scale,

    texture.height * scale

};

}

character.h:

#include “raylib.h”

#include “CharacterBase.h”

class Character : public CharacterBase

{

public:

Character(int winWidth, int winHeight);

Vector2 getWorldPos() { return worldPos; }

void tick(float deltaTime);

void undoMovement();

Rectangle GetCollisionRec();

private:

Texture2D texture{LoadTexture("characters/knight_idle_spritesheet.png")};

Texture2D Idle{LoadTexture("characters/knight_idle_spritesheet.png")};

Texture2D Run{LoadTexture("characters/knight_run_spritesheet.png")};

Vector2 screenPos{};

Vector2 worldPos{};

Vector2 worldPosOld{};

float rightLeft{1.f};

float runningTime{};

int frame{};

int maxFrames{6};

float updateTime{1.f / 12.f};

float speed{4.f};

float width{};

float height{};

float scale{4.0f};

};

character.cpp:

#include “Character.h”

#include “raymath.h”

Character::Character(int winWidth, int winHeight)

{

width = texture.width / maxFrames;

height = texture.height;

screenPos = {static_cast<float>(winWidth) / 2.0f - scale * (0.5f * width),

             static_cast<float>(winHeight) / 2.0f - scale * (0.5f * height)

};

}

void Character::tick(float deltaTime)

{

worldPosOld = worldPos;

Vector2 direction{};

if (IsKeyDown(KEY_A))

    direction.x -= 1.0;

if (IsKeyDown(KEY_D))

    direction.x += 1.0;

if (IsKeyDown(KEY_W))

    direction.y -= 1.0;

if (IsKeyDown(KEY_S))

    direction.y += 1.0;

if (Vector2Length(direction) != 0.0)

{

    worldPos = Vector2Add(worldPos, Vector2Scale(Vector2Normalize(direction), speed));

    direction.x < 0.f ? rightLeft = -1.f : rightLeft = 1.f;

    texture = Run;

}

else

{

    texture = Idle;

}

runningTime += deltaTime;

if (runningTime >= updateTime)

{

    frame++;

    runningTime = 0.f;

    if (frame > maxFrames)

        frame = 0;

}

Rectangle source{frame * width, 0.f, rightLeft * width, height};

Rectangle dest{screenPos.x, screenPos.y, scale * width, scale * height};

DrawTexturePro(texture, source, dest, Vector2(), 0.f, WHITE);

}

void Character::undoMovement()

{

worldPos = worldPosOld;

}

Rectangle Character::GetCollisionRec()

{

return Rectangle{

    screenPos.x,

    screenPos.y,

    width * scale,

    height *scale

};

}

Hi Stew,

While I can’t look at this at this moment (i’ll be able to later today), can you describe what error or unexpected behaviour you’re getting? That’ll help me identify the cause.

Hi Stew,

Just to update you on my progress. One of the first errors I see is that #ifdef should be ifndef.

The first one asks if the specifier has been defined already and the other asks if it has not been defined yet. Since CHARACTER_BASE_H Would not have been defined the compiler then ignores everything after it, causing compilation errors for the rest of the code.

There are other errors, but this looks like it may be incomplete towards transitioning code into the CharacterBase class.

I am not sure what the other errors are, I did the whole tutorial and I did not find anything wrong, here’s my terminal if it helps:

  • Executing task in folder vscode-template-main: C:/raylib/w64devkit/bin/mingw32-make.exe RAYLIB_PATH=C:/raylib/raylib PROJECT_NAME=ClassyClash OBJS=*.cpp BUILD_MODE=DEBUG

mingw32-make ClassyClash
make[1]: Entering directory ‘C:/Users/User/Desktop/.vscode/Classy Clash/vscode-template-main’
g++ -o ClassyClash *.cpp -Wall -std=c++14 -D_DEFAULT_SOURCE -Wno-missing-braces -g -O0 C:/raylib/raylib/src/raylib.rc.data -I. -IC:/raylib/raylib/src -IC:/raylib/raylib/src/external -L. -LC:/raylib/raylib/src -LC:/raylib/raylib/src -lraylib -lopengl32 -lgdi32 -lwinmm -DPLATFORM_DESKTOP
In file included from ClassyClash.cpp:5:
Enemy.h:30:2: error: expected ‘;’ after class definition
30 | }
| ^
| ;
In file included from Enemy.cpp:1:
Enemy.h:4:1: error: new types may not be defined in a return type
4 | class Enemy : public CharacterBase
| ^~~~~
Enemy.h:4:1: note: (perhaps a semicolon is missing after the definition of ‘Enemy’)
Enemy.h:4:1: error: return type specification for constructor invalid
Enemy.h: In constructor ‘Enemy::Enemy(Vector2, Texture2D, Texture2D)’:
Enemy.h:18:13: warning: ‘Enemy::worldPos’ will be initialized after [-Wreorder]
18 | Vector2 worldPos{};
| ^~~~~~~~
Enemy.h:14:15: warning: ‘Texture2D Enemy::texture’ [-Wreorder]
14 | Texture2D texture{LoadTexture(“characters/knight_idle_spritesheet.png”)};
| ^~~~~~~
Enemy.cpp:3:1: warning: when initialized here [-Wreorder]
3 | Enemy::Enemy(Vector2 pos, Texture2D idle_texture, Texture2D run_texture) :
| ^~~~~
In file included from Prop.cpp:1:
Prop.h: In constructor ‘Prop::Prop(Vector2, Texture2D)’:
Prop.h:11:13: warning: ‘Prop::worldPos’ will be initialized after [-Wreorder]
11 | Vector2 worldPos{};
| ^~~~~~~~
Prop.h:10:15: warning: ‘Texture2D Prop::texture’ [-Wreorder]
10 | Texture2D texture{};
| ^~~~~~~
Prop.cpp:4:1: warning: when initialized here [-Wreorder]
4 | Prop::Prop(Vector2 pos, Texture2D tex) :
| ^~~~
make[1]: *** [Makefile:392: ClassyClash] Error 1
make[1]: Leaving directory ‘C:/Users/User/Desktop/.vscode/Classy Clash/vscode-template-main’
make: *** [Makefile:388: all] Error 2

  • The terminal process “C:\raylib\w64devkit\bin\mingw32-make.exe ‘RAYLIB_PATH=C:/raylib/raylib’, ‘PROJECT_NAME=ClassyClash’, ‘OBJS=*.cpp’, ‘BUILD_MODE=DEBUG’” terminated with exit code: 2.

  • Terminal will be reused by tasks, press any key to close it.

  • Executing task in folder vscode-template-main: C:/raylib/w64devkit/bin/mingw32-make.exe RAYLIB_PATH=C:/raylib/raylib PROJECT_NAME=ClassyClash OBJS=*.cpp BUILD_MODE=DEBUG

mingw32-make ClassyClash
make[1]: Entering directory ‘C:/Users/User/Desktop/.vscode/Classy Clash/vscode-template-main’
g++ -o ClassyClash *.cpp -Wall -std=c++14 -D_DEFAULT_SOURCE -Wno-missing-braces -g -O0 C:/raylib/raylib/src/raylib.rc.data -I. -IC:/raylib/raylib/src -IC:/raylib/raylib/src/external -L. -LC:/raylib/raylib/src -LC:/raylib/raylib/src -lraylib -lopengl32 -lgdi32 -lwinmm -DPLATFORM_DESKTOP
In file included from Character.cpp:1:
Character.h:5:1: error: expected class-name before ‘{’ token
5 | {
| ^
CharacterBase.cpp:3:1: error: ‘BaseCharacter’ does not name a type
3 | BaseCharacter::BaseCharacter()
| ^~~~~~~~~~~~~
In file included from ClassyClash.cpp:3:
Character.h:5:1: error: expected class-name before ‘{’ token
5 | {
| ^
In file included from ClassyClash.cpp:5:
Enemy.h:5:1: error: expected class-name before ‘{’ token
5 | {
| ^
Enemy.h:30:2: error: expected ‘;’ after class definition
30 | }
| ^
| ;
In file included from Enemy.cpp:1:
Enemy.h:5:1: error: expected class-name before ‘{’ token
5 | {
| ^
Enemy.h:4:1: error: new types may not be defined in a return type
4 | class Enemy : public BaseCharacter
| ^~~~~
Enemy.h:4:1: note: (perhaps a semicolon is missing after the definition of ‘Enemy’)
Enemy.h:4:1: error: return type specification for constructor invalid
Enemy.h: In constructor ‘Enemy::Enemy(Vector2, Texture2D, Texture2D)’:
Enemy.h:18:13: warning: ‘Enemy::worldPos’ will be initialized after [-Wreorder]
18 | Vector2 worldPos{};
| ^~~~~~~~
Enemy.h:14:15: warning: ‘Texture2D Enemy::texture’ [-Wreorder]
14 | Texture2D texture{LoadTexture(“characters/knight_idle_spritesheet.png”)};
| ^~~~~~~
Enemy.cpp:3:1: warning: when initialized here [-Wreorder]
3 | Enemy::Enemy(Vector2 pos, Texture2D idle_texture, Texture2D run_texture) :
| ^~~~~
In file included from Prop.cpp:1:
Prop.h: In constructor ‘Prop::Prop(Vector2, Texture2D)’:
Prop.h:11:13: warning: ‘Prop::worldPos’ will be initialized after [-Wreorder]
11 | Vector2 worldPos{};
| ^~~~~~~~
Prop.h:10:15: warning: ‘Texture2D Prop::texture’ [-Wreorder]
10 | Texture2D texture{};
| ^~~~~~~
Prop.cpp:4:1: warning: when initialized here [-Wreorder]
4 | Prop::Prop(Vector2 pos, Texture2D tex) :
| ^~~~
make[1]: *** [Makefile:392: ClassyClash] Error 1
make[1]: Leaving directory ‘C:/Users/User/Desktop/.vscode/Classy Clash/vscode-template-main’
make: *** [Makefile:388: all] Error 2

  • The terminal process “C:\raylib\w64devkit\bin\mingw32-make.exe ‘RAYLIB_PATH=C:/raylib/raylib’, ‘PROJECT_NAME=ClassyClash’, ‘OBJS=*.cpp’, ‘BUILD_MODE=DEBUG’” terminated with exit code: 2.
  • Terminal will be reused by tasks, press any key to close it.

mingw32-make ClassyClash
make[1]: Entering directory ‘C:/Users/User/Desktop/.vscode/Classy Clash/vscode-template-main’
g++ -o ClassyClash *.cpp -Wall -std=c++14 -D_DEFAULT_SOURCE -Wno-missing-braces -g -O0 C:/raylib/raylib/src/raylib.rc.data -I. -IC:/raylib/raylib/src -IC:/raylib/raylib/src/external -L. -LC:/raylib/raylib/src -LC:/raylib/raylib/src -lraylib -lopengl32 -lgdi32 -lwinmm -DPLATFORM_DESKTOP
In file included from ClassyClash.cpp:5:
Enemy.h:30:2: error: expected ‘;’ after class definition
30 | }
| ^
| ;
In file included from Enemy.cpp:1:
Enemy.h:4:1: error: new types may not be defined in a return type
4 | class Enemy : public BaseCharacter
| ^~~~~
Enemy.h:4:1: note: (perhaps a semicolon is missing after the definition of ‘Enemy’)
Enemy.h:4:1: error: return type specification for constructor invalid
Enemy.h: In constructor ‘Enemy::Enemy(Vector2, Texture2D, Texture2D)’:
Enemy.h:18:13: warning: ‘Enemy::worldPos’ will be initialized after [-Wreorder]
18 | Vector2 worldPos{};
| ^~~~~~~~
Enemy.h:14:15: warning: ‘Texture2D Enemy::texture’ [-Wreorder]
14 | Texture2D texture{LoadTexture(“characters/knight_idle_spritesheet.png”)};
| ^~~~~~~
Enemy.cpp:3:1: warning: when initialized here [-Wreorder]
3 | Enemy::Enemy(Vector2 pos, Texture2D idle_texture, Texture2D run_texture) :
| ^~~~~
In file included from Prop.cpp:1:
Prop.h: In constructor ‘Prop::Prop(Vector2, Texture2D)’:
Prop.h:11:13: warning: ‘Prop::worldPos’ will be initialized after [-Wreorder]
11 | Vector2 worldPos{};
| ^~~~~~~~
Prop.h:10:15: warning: ‘Texture2D Prop::texture’ [-Wreorder]
10 | Texture2D texture{};
| ^~~~~~~
Prop.cpp:4:1: warning: when initialized here [-Wreorder]
4 | Prop::Prop(Vector2 pos, Texture2D tex) :
| ^~~~
make[1]: *** [Makefile:392: ClassyClash] Error 1
make[1]: Leaving directory ‘C:/Users/User/Desktop/.vscode/Classy Clash/vscode-template-main’
make: *** [Makefile:388: all] Error 2

  • The terminal process “C:\raylib\w64devkit\bin\mingw32-make.exe ‘RAYLIB_PATH=C:/raylib/raylib’, ‘PROJECT_NAME=ClassyClash’, ‘OBJS=*.cpp’, ‘BUILD_MODE=DEBUG’” terminated with exit code: 2.
  • Terminal will be reused by tasks, press any key to close it.
    (note, I have renamed it BaseCharacter, not CharacterBase and I did do that ifndef)

Judging by the last error (the rest of your terminal seems to show other attempts to compile). That error should be resolved by the solution I posted above.

I Changed it to ifndef and nothing changed when I made that last post, I am going to restart but this code will still exsist, if you find out what I did wrong within 20 days I will be glad but for now I will be doing it from almost scratch
WORKED BUT I THINK HE COULD HAVE BEEN CORRECT

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms