我有一个结构,用于在标题中声明纹理,精灵,音乐,字体,声音等。然后我使用相应的.cpp文件将所述纹理设置为相应的精灵,依此类推。
我在main中创建了struct对象,并调用了该函数。但是当我构建我的程序时,我得到了一个百万"未声明的标识符"错误。我究竟做错了什么?这是我的代码:
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);
//...
}
答案 0 :(得分:2)
首先,我建议您阅读有关标题文件的something。
resources.h
确实不包含所用标识符的声明。
您必须使用包含这些声明的#include
头文件,以便编译器能够看到它们。请阅读SFML declaration以查看需要包含哪些模块,并#include
将其添加到标题文件中,现在您只将它们包含在main.cpp
中, main.cpp
包含标识符声明。当编译器读取resources.cpp
时,它没有看到那些声明,因此undeclared identifier
错误。
resources.h应该如下所示:
//...
#include "entity.h"
#include <SFML/Graphics.hpp>
#include <Whatever.hpp>
struct resources
{
//...