问题
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);
//...
}
回答1:
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
{
//...
来源:https://stackoverflow.com/questions/38085977/problems-loading-resources-from-function-c-sfml