0

I'm trying to build a game in C++, and I got this error while compiling my code.

src/include/rendering_engine/ECS/sprite_component.h: In constructor 'SpriteComponent::SpriteComponent(const char*, int, int)':
src/include/rendering_engine/ECS/sprite_component.h:19:23: error: 'TextureManager' has not been declared
             texture = TextureManager::LoadTexture(path);
                       ^~~~~~~~~~~~~~

I did include the file where the TextureManager class is located, but for some reason it still says it has not been declared. This is my code:

textures.h:

#pragma once

#include "game.h" // by including game.h I am also including SDL2. 

class TextureManager {
    public:
        static SDL_Texture* LoadTexture(const char* filename);
        static void Draw(SDL_Texture *tex, SDL_Rect src, SDL_Rect dest);
};

components.h:

#pragma once

#include "SDL2/SDL.h"
#include "../textures.h"
#include "ECS.h"
#include "position_component.h"
#include "sprite_component.h"

sprite_component.h:

#pragma once

#include "components.h"
#include "../textures.h"

class SpriteComponent : public Component {
    private:
        int width, height;
        PositionComponenet *position;
        SDL_Texture *texture;
        SDL_Rect srcRect, destRect;
    
    public:
        SpriteComponent() = default;
        SpriteComponent(const char* path, int texW, int texH) {
            width = texW;
            height = texH;

            texture = TextureManager::LoadTexture(path); // This is where the error is occurring
        }

        void init() override {
            position = &entity->getComponent<PositionComponenet>();

            srcRect.x = srcRect.y = 0;
            srcRect.w = width;
            srcRect.h = height;

            destRect.w = srcRect.w * 2;
            destRect.h = srcRect.h * 2;
        }

        void update() override {
            destRect.x = position->x();
            destRect.y = position->y();
        }

        void draw() override {
            TextureManager::Draw(texture, srcRect, destRect);
        }
};

In VS Code, it doesn't recognize any errors. This only occurs while compiling (I use MinGW). I was hoping that it would compile normally. For more details, here is the file structure:

project
|→ ECS
|  |→ components.h
|  |→ sprite_component.h
|
|→ textures.h
|→ game.h

Note that there are more files than just that, but I think those are the only relevant files. Also I'm new to C++, so while this might be an easy issue to resolve, I don't know how.

Keshav V.
  • 73
  • 7

0 Answers0