Header problems

Hello! I'm making a program with lots of headers, but it doesn't link correctly, displaying the "not declared in this scope" error. How should i #include those headers correctly?

My files:
1
2
3
4
5
//main.cpp

using namespace std;

#include "world.h" 

1
2
3
4
5
6
7
8
9
//world.h

#ifndef WORLD_H_INCLUDED
#define WORLD_H_INCLUDED

#include "sfml_data.h"
#include "tile.h"
#include "entity.h"
#include "filedata.h" 

1
2
3
4
5
6
7
8
9
10
11
12
//sfml_data.h

#ifndef SFML_DATA_H_INCLUDED
#define SFML_DATA_H_INCLUDED

#include <SFML/Graphics.hpp>

sf::RenderWindow window;

const int tekstur_num=10;

sf::Texture teksturos[tekstur_num];

1
2
3
4
5
6
7
8
9
10
11
//tile.h

#ifndef TILE_H_INCLUDED
#define TILE_H_INCLUDED

#include <SFML/Graphics.hpp>

class Tile
{
    //some code
}

1
2
3
4
5
6
7
8
9
10
11
12
//tile.cpp

#include "tile.h"

//some code

sprites[i].setTexture(teksturos[picnum]);

//some code

window.draw(sprites[current_anim]);


The problem is that it says window and teksturos is not declared in this scope.
Last edited on
The problem is that it says window and teksturos is not declared in this scope.


They're not.

tile.cpp includes tile.h
tile.h includes SFML/Graphics.hpp and defines the tile class.


tile.cpp does not include ANYTHING that defines or includes 'window' or 'teksturos'. For that, it would need to include 'sfml_data' since that is where they are defined.


However... that will open up other problems because then you'd be defining those objects in multiple cpp files. So you'll get a linker error saying the objects are defined multiple times.



My advice: find a way to do this without using global variables.
But i need to have those variables global, because they need to be loaded to memory only once, otherwise, loading them to memory from a file each time a tile is generated, it would slow down a performance very much!
Maybe i should try to make some class and load those sfml-data variables there?
Last edited on
Using extern keyword in header file is the best option!
Topic archived. No new replies allowed.