简体   繁体   English

如何在一个 class 中初始化命名空间中的所有变量

[英]How to initialize all variables from a namespace in one class

I am making a small game using SFML.我正在使用 SFML 制作一个小游戏。 I want to load all my resources in one go when the game launches, so every time a new entity is created new resources don't need to be loaded to memory slowing down the game.我想在游戏启动时将所有资源加载到一个 go 中,因此每次创建新实体时都不需要将新资源加载到 memory 中,从而减慢游戏速度。

I have made a header file with a namespace for each kind of resource I need:我制作了一个 header 文件,其中包含我需要的每种资源的命名空间:

#pragma once

#include <SFML/Graphics.hpp>

#include <iostream>

#include "spritesheet.h"

namespace PlayerSprite {
    extern Spritesheet playerSpritesheet;
    extern sf::Sprite sp1, sp2, sp3, sp4, sp5, sp6;
    extern int width = 16;
    extern int height = 16;
}

namespace ButtonSprite {
    extern Spritesheet buttonSpritesheet;
    extern sf::Sprite button;
    extern int width = 32;
    extern int height = 16;
}

class Resources {
    public:
        Resources() = default;
        void loadResources();
};

Now I don't know much about namespaces only very basic stuff, this is the first reason to use one.现在我对命名空间了解不多,只是非常基本的东西,这是使用命名空间的第一个原因。

Now in the cpp file:现在在 cpp 文件中:

#include <iostream>

#include "../headers/resources.h"

void Resources::loadResources() {
    // PLAYER
    PlayerSprite::playerSpritesheet.setSprite("./res/img/tiles.png");
    PlayerSprite::sp1 = PlayerSprite::playerSpritesheet.getSprite(0, 0, PlayerSprite::width, 
    PlayerSprite::height);
    PlayerSprite::sp2 = PlayerSprite::playerSpritesheet.getSprite(32, 0, PlayerSprite::width, 
    PlayerSprite::height);
    PlayerSprite::sp3 = PlayerSprite::playerSpritesheet.getSprite(48, 0, PlayerSprite::width, 
    PlayerSprite::height);
    PlayerSprite::sp4 = PlayerSprite::playerSpritesheet.getSprite(64, 0, PlayerSprite::width, 
    PlayerSprite::height);
    PlayerSprite::sp5 = PlayerSprite::playerSpritesheet.getSprite(80, 0, PlayerSprite::width, 
    PlayerSprite::height);
    PlayerSprite::sp6 = PlayerSprite::playerSpritesheet.getSprite(0, 16, PlayerSprite::width, 
    PlayerSprite::height);
    // PLAYER

    // BUTTONS
    ButtonSprite::buttonSpritesheet.setSprite("./res/img/tiles.png");
    ButtonSprite::button = ButtonSprite::buttonSpritesheet.getSprite(48, 16, 32, 16);
    // BUTTONS
}

I then call this function in the main game file, but when I run I get this error in the player class:然后我在主游戏文件中调用此 function,但是当我运行时,我在播放器 class 中收到此错误:

undefined reference to 'PlayerSprite:sp1'

I don't know if this is because of the namespace declaration, or me coding the system wrong.我不知道这是因为命名空间声明,还是我对系统的编码错误。


After looking at some comments on this question I have added this:在查看了有关此问题的一些评论后,我添加了以下内容:

void Resources::allocateStorage() {
    sf::Sprite PlayerSprite::spritesheet;
    sf::Sprite PlayerSprite::sp1;
}

In the cpp file, but I am now getting this error:在 cpp 文件中,但我现在收到此错误:

unqualified-id in declaration before ';' token unqualified-id in declaration before ';' token on the 1st line after the implementation of the function.执行 function 后的第 1 行unqualified-id in declaration before ';' token

The problem seems to be that the variables are not actually defined anywhere, just declared.问题似乎是变量实际上并没有在任何地方定义,只是声明了。 When using extern , the compiler will assume that this variable exists, so the compiler can continue on assuming that the variable exists.使用extern时,编译器将假定该变量存在,因此编译器可以继续假定该变量存在。 The linker will then come in, and see where that variable exists.然后 linker 将进入,并查看该变量存在的位置 If it does not, you get an undefined reference error.如果没有,您会收到未定义的引用错误。 To fix this, you need to define the variables somewhere.要解决此问题,您需要在某处定义变量。

For example, in say resources.cpp , you would have:例如,在说resources.cpp中,您将拥有:

#include "../headers/resources.h"

sf::Sprite PlayerSpriter::sp1;
sf::Sprite PlayerSpriter::sp2;
sf::Sprite PlayerSpriter::sp3;
// etc...


void Resources::loadResources() { 
    // …
}

This is in addition to what you already have in your header.这是您在 header 中已有的内容的补充。

NOTE: From your edit, this would occur outside any function.注意:根据您的编辑,这将发生在任何 function 之外。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM