0

I have a stucture that I use to declare Textures, Sprites, Music, Fonts, Sounds, etc in a header. I then use a corresponding .cpp file to set said textures to their corresponding sprites, and so on.

I have created the struct object in main, and called the function. But When I build my program, I get a million "undeclared identifier" errors. What am I doing wrong? Here's my code:

resources.h:

#ifndef __SFML__resources__
#define __SFML__resources__

#include "SFML/Audio.hpp"
#include "SFML/Graphics.hpp"

struct resources
{
float opacity = 1.0;

sf::Texture texturePlayer;
sf::Texture textureSettingsMenu;
sf::Sprite spriteSaveMenu;
sf::Sprite spriteSettingsMenu;
//start screen texture
sf::Texture textureStart;
//gamestate background sprite
sf::Sprite spriteScreen;
sf::Font font;
sf::Font font2;
int loadResources();
// hundreds more lines of declarations below
//...
};
#endif

resources.cpp:

#include "resources.h"


int resources::loadResources()
{
if (!textureSaveMenu.loadFromFile("images/magicmenu3.png"))
    return EXIT_FAILURE;


if (!textureSettingsMenu.loadFromFile("images/inventorymenu.png"))
    return EXIT_FAILURE;

spriteSaveMenu.setTexture(textureSaveMenu);
spriteSettingsMenu.setTexture(textureSettingsMenu);

if (!textureStart.loadFromFile("images/skyocean.png"))
    return EXIT_FAILURE;

if (!font.loadFromFile("fonts/arial.ttf"))
    return EXIT_FAILURE;

if (!font2.loadFromFile("fonts/dominican.ttf"))
    return EXIT_FAILURE;

if (!musicBattle.openFromFile("audio/musicBattle.ogg"))
    return EXIT_FAILURE;
musicBattle.setVolume(20);
musicBattle.setLoop(true);


if (!musicOpening.openFromFile("audio/maintheme.ogg"))
    return EXIT_FAILURE;
musicOpening.setVolume(30);
musicOpening.setLoop(true);

if (!musicDefeat.openFromFile("audio/defeat.ogg"))
    return EXIT_FAILURE;
musicDefeat.setVolume(50);
musicDefeat.setLoop(true);

if (!musicForest.openFromFile("audio/forest.ogg"))
    return EXIT_FAILURE;
musicForest.setVolume(10);
musicForest.setLoop(true);

if (!musicWorldMap.openFromFile("audio/bluefields.ogg"))
    return EXIT_FAILURE;
musicWorldMap.setVolume(40);
musicWorldMap.setLoop(true);

if (!musicVillage.openFromFile("audio/welcomehome.ogg"))
    return EXIT_FAILURE;
musicVillage.setVolume(40);
musicVillage.setLoop(true);

//every single declared resource is set within this function
//but the function is too large to display fully here
}

main.cpp:

 #include "SFML/Audio.hpp"
 #include "SFML/Graphics.hpp"
 #include <iostream>
 #include "random.h"
 #include "player.h"
 #include "entity.h"
 #include "projectile.h"
 #include "enemy.h"
 #include "textDisplay.h"
 #include "pickup.h"
 #include "wall.h"
 #include "sword.h"
 #include "npc.h"
 #include "battlemenu.h"
 #include "cursor.h"
 #include "name.h"
 #include "itemshop.h"
 #include "characterselection.h"
 #include "mainmenu.h"
 #include "exp.h"
 #include "drops.h"
 #include "weaponshop.h"
 #include "armorshop.h"
 #include "startmenu.h"
 #include "room.h"
 #include "resources.h"


int main()
{
//create the main window
sf::RenderWindow window(sf::VideoMode(720, 500), "Sky Ocean");
window.setFramerateLimit(60);
window.setKeyRepeatEnabled(false);

//View
sf::View view1(sf::FloatRect(200, 200, 300, 200));
view1.setSize(sf::Vector2f(window.getSize().x, window.getSize().y));
view1.setCenter(sf::Vector2f(view1.getSize().x / 2, view1.getSize().y / 2));
window.setView(view1);

//load resources
struct resources resources1;
resources1.loadResources();

//class object
class player Player1;
Player1.sprite.setTexture(texturePlayer);

class player Player2;
Player2.sprite.setTexture(texturePlayer);
Player2.rect.setScale(1.6, 1.6);
Player2.sprite.setScale(1.6, 1.6);

//...
}
Ivan Rubinson
  • 3,001
  • 4
  • 19
  • 48
  • 2
    "#define \_\_SFML\_\_resources\_\_" - Don't use reserved names in your own code. Any symbol starting with underscore followed by a capital letter and any symbol containing two underscore characters in succession are reserved for the implementation and must not be used in your code. – Jesper Juhl Jun 28 '16 at 20:50
  • why is it a bad thing to do this, is it what is causing my error? what would be the correct way to name it? What's the difference? – anarchyistheanswer Jun 28 '16 at 20:54
  • 1
    It is not what is causing your current problem. I just thought I'd let you know. It is bad since such names are officially reserved and thus your names may clash with names used by your compiler or standard library. It is also in some cases undefined behaviour to `#undef` such names and there are other potential headaches. So I just wanted to say, "don't use reserved names" :-). Since you asked; personally I'd probably have named that header guard "MY_PROJECT_SFML_RESOURCES_H" if "sfml_resources.h" was the header it was protecting. – Jesper Juhl Jun 28 '16 at 21:00
  • 1
    How are you building your program? What IDE or commandline? – kfsone Jun 28 '16 at 21:18
  • 1
    Do you tell your compiler where to find the SFML headers? – Jesper Juhl Jun 28 '16 at 21:35
  • I am using visual studio 2013 – anarchyistheanswer Jun 28 '16 at 22:26
  • I don't think I tell the compiler where to find them, I #include the headers from the files inside my project folder – anarchyistheanswer Jun 28 '16 at 22:27
  • 1
    Is the SFML library installed in the default place? – Galik Jun 28 '16 at 23:33
  • 1
    Did you try using angle brackets `` rather than quotes? – Galik Jun 28 '16 at 23:34
  • Just tried angle brackets, it didn't solve the problem – anarchyistheanswer Jun 28 '16 at 23:37
  • SFML library is installed in the right place, I previously had all of these declarations inside of main and it worked just fine – anarchyistheanswer Jun 28 '16 at 23:38
  • Why do you initialize the resources in a function and not the constructor? You should use RAII. Also, lines like `setTexture(texturePlayer)` are wrong. You should use `setTexture(resources1.texturePlayer)` – Ivan Rubinson Jun 29 '16 at 04:33

1 Answers1

2

Firstly, I recommend you to read something about header files.


resources.h indeed doesn't contain declaration of identifiers used.

You will have to #include header file which contains those declarations so compiler will be able to see them. Please read SFML declaration to see which modules need to be included and #include them into header file, now you're including them only into main.cpp so only main.cpp contains declaration of identifiers. When compiler reads resources.cpp it doesn't see those declaration, henceundeclared identifier error.

resources.h for example should look like this:

//...

#include "entity.h"

#include <SFML/Graphics.hpp>
#include <Whatever.hpp>

struct resources
{

//...
Community
  • 1
  • 1
PcAF
  • 1,975
  • 12
  • 20
  • I've already tried this where I include the sfml/graphics.hpp inside of resources.h to no avail – anarchyistheanswer Jun 28 '16 at 23:26
  • 2
    `#include` header files in every file where they're needed (whether it's source or header file). – PcAF Jun 28 '16 at 23:27
  • 1
    @anarchyistheanswer in `entity.h` you probably missed something (`Audio.hpp`?), but as I wrote "#include header files in every file where they're needed (whether it's source or header file)" – PcAF Jun 28 '16 at 23:28
  • Even when I include these inside of resources.h the identifiers are still undeclared, I updated my answer to show what i'm talking about – anarchyistheanswer Jun 28 '16 at 23:30
  • The compiler says that each occurrence of the declared textures, sprites, fonts, etc inside of the struct are undeclared. When it comes across texturePlayer inside of main() for instance, texturePlayer is undeclared – anarchyistheanswer Jun 28 '16 at 23:39
  • 1
    I'll bet that `texturePlayer` was declared in `entity.h` which is not included in `main.cpp`. `#include` header files everywhere where needed. – PcAF Jun 28 '16 at 23:45
  • nope, texturePlayer is declared inside of resources.h and I updated the `#include` headers inside main to be exactly what is in my program. Also I edited resources.h to show clearly that texturePlayer is declared – anarchyistheanswer Jun 28 '16 at 23:49
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/115906/discussion-between-pcaf-and-anarchyistheanswer). – PcAF Jun 29 '16 at 00:00