Search code examples
c++compiler-errors2dassetssfml

C++ Thor library - problem with using resource loader class ( ' ' does not name a type)


I have been recently practicing managing multiple objects and drawing them in C++ using SFML library. I wanted my textures and future resources to be more reusable so I decided to make use of Thor library which suits my needs really well.

So I've written first few lines of code based on what you can find in this tutorial and the compiler always says:

main.cpp|12|error: 'textures_holder' does not name a type

This line gives an error :

textures_holder.acquire("Dirt", thor::Resources::fromFile<sf::Texture>("Textures\\dirt_block.png"));

I'm using Code::Blocks IDE with MinGW compiler and SFML 2.5.0.

Here's my main.cpp and the header file which contains extern object :

//...
#include <Thor/Resources.hpp>
#include "Dirt_Block.h"

using namespace std;

//Adding textures to the texture library 
//THIS LINE GIVES AN ERROR
textures_holder.acquire("Dirt", thor::Resources::fromFile<sf::Texture>("Textures\\dirt_block.png"));

//Rest of code...

Dirt_Block.h (only the upper part) :

#ifndef DIRT_BLOCK_H
#define DIRT_BLOCK_H

#include <SFML\Graphics.hpp>
#include <vector>
#include <Thor/Resources.hpp>
#include <Thor/Resources/SfmlLoaders.hpp>

extern sf::Vector2u screenRes;
extern thor::ResourceHolder<sf::Texture, std::string> textures_holder;
//Rest of the code

I'd like to know what is causing this error and maybe help others who may experience similiar frustrating problems. Thanks for help.

EDIT :

As suggested in the comment I've declared a few extern int variables in the Dirt_Block.h so now it looks like this :

//...
extern int test_int_up;
extern sf::Vector2u screenRes;
extern thor::ResourceHolder<sf::Texture, std::string> textures_holder;
extern int test_int_d;
//...

And then assinged to them some value in main.cpp :

//...
test_int_up = 55;
test_int_d = 55;
//Adding textures to the texture library
textures_holder.acquire("Dirt", thor::Resources::fromFile<sf::Texture>("Textures\\dirt_block.png"));
//...

But the compiler gives error :

main.cpp|9|error: 'test_int_up' does not name a type
main.cpp|10|error: 'test_int_d' does not name a type
main.cpp|12|error: 'textures_holder' does not name a type

Solution

  • Much less distracting to see what your problem is without all the extraneous code!

    C++ programs don't start from the top of the file and run code down to the bottom. They start at the main(), and control flow proceeds from there, with one thing triggering another.

    (Note: That doesn't take into account global constructor ordering, which does go in order of declaration--but you have no guarantee of the order declarations from "different files" might run in.)

    Point being, you can't just make random function or method calls in the middle of a file. That's where you put declarations. You have to be inside of a function or method to make calls, e.g.

    int main() {
        textures_holder.acquire(
            "Dirt",
            thor::Resources::fromFile<sf::Texture>("Textures\\dirt_block.png")
        );
        ...
    }