0

I've run into a problem that's really annoying and bothering me. This is the code:

block.hpp:

#pragma once

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

#include "../global/global.hpp"

class Block{
    SDL_Texture* texture;
    SDL_Rect position;
    public:
    void setTexture(SDL_Texture* _texture);
    void setPosition(SDL_Rect _position);
    void render();
    void destroy();
};

block.cpp:

#include "block.hpp"

void Block::setTexture(SDL_Texture* _texture){
    texture = _texture;
}

void Block::setPosition(SDL_Rect _position){
    position = _position;
}

void Block::render(){
    SDL_RenderCopy(g_renderer,texture,nullptr,&position);
}

solid.hpp:

#pragma once

#include "block.hpp"

class Solid : public Block{
    public:
    Solid();
    void setPosition(SDL_Rect _position);
    void render();
};

solid.cpp:

#include "solid.hpp"

Solid::Solid(){
    Block::setTexture(g_assets.textures["cegla"]);
}

void Solid::setPosition(SDL_Rect _position){
    Block::setPosition(_position);
}

void Solid::render(){
    Block::render();
}

Error:

In file included from src/block/../global/../block/blocks.hpp:3,
                 from src/block/../global/../map.hpp:8,
                 from src/block/../global/../assetmanager.hpp:9,
                 from src/block/../global/global.hpp:6,
                 from src/block/block.hpp:6,
                 from src/block/block.cpp:1:
src/block/../global/../block/solid.hpp:5:27: error: expected class-name before ‘{’ token
    5 | class Solid : public Block{

Please help.I don't know what could be the reason, I tried replacing #include "block.hpp" with class Block, nothing helped

Mysz nocny
  • 31
  • 3
  • 3
    What is `global.hpp`? Does it by any chance `#include "block.hpp"` or `solid.hpp`? – Yksisarvinen Apr 11 '23 at 11:20
  • No need to include iostream in Block.hpp. – kiner_shah Apr 11 '23 at 11:20
  • looks like circular dependencies. The code you show us did not contain the problem. I expect that some files in the include order higher than block.hpp include solid.hpp. – Klaus Apr 11 '23 at 11:21
  • 2
    You're including unnecessary headers in headers, which often leads to cicularity problems. For instance, "block.hpp" does not need "../global/global.hpp" or . The former should be in "block.cpp", the latter shouldn't be included there at all. – molbdnilo Apr 11 '23 at 11:22
  • I expect your bug is a circular header loop caused by: `#include "../global/global.hpp"` – drescherjm Apr 11 '23 at 12:59
  • Does this answer your question? [Resolve build errors due to circular dependency amongst classes](https://stackoverflow.com/questions/625799/resolve-build-errors-due-to-circular-dependency-amongst-classes) – drescherjm Apr 11 '23 at 13:00

1 Answers1

2

The include chain displayed in the compile error tells us what's wrong.

block.hpp includes global.hpp which includes assetmanager.hpp which include map.hpp which includes blocks.hpp which includes solid.hpp which includes block.hpp.

But this very last include is already in the include chain, so it is ignored by the #pragma once. This means that when the compiler gets to the Solid class definition, it hasn't seen the Block class definition yet, which leads to the error you're getting.

You need to reorganize and clean up your headers.

Sebastian Redl
  • 69,373
  • 8
  • 123
  • 157