类中的访问结构对象变量-C ++

迪诺·马奇诺(DeanoMachino)

我会尽力解释这一点...

基本上,我正在为GBA游戏编写此程序,并且试图从类中更改struct实例的成员变量。这是代码,省略了不必要的部分:

播放器

#include "player.h"                // Line 1
#include "BgLayerSettings.h"
player::player(){
    x = 16;
    y = 16;
    health = 5;
    direction = LEFT;
    dead = false;
}

player::~player(){
}

// Omitted unrelated code

void player::ScrollScreen(){       // Line 99
    if(x>((240/2)-8)){
        BACKGROUND_2.h_offset += x-((240/2)-8);
    }
}

播放器

#include <stdint.h>                // Line 1
#include <stdlib.h>
#include <string.h>
#include "gba.h"
#include "font.h"
#pragma once

class player {
public:
    player();
    ~player();

    unsigned int x;
    unsigned int y;

    void ScrollScreen();
};

BgLayerSettings.cpp

#include "player.h"                // Line 1
#include "BgLayerSettings"

BgLayerSettings::BgLayerSettings(){
    charblock = 0;
    screenblock = BLANK;
    v_offset = 0;
    h_offset = 0;
}

BgLayerSettings::~BgLayerSettings(){

}

BgLayerSettings.h

#include <stdint.h>                // Line 1
#include <stdlib.h>
#include <string.h>
#include "gbs.h"
#include "font.h"
#pragma once

enum BACKGROUND {bg0=0, bg1, bg2, bg3, bg4, bg5, bg6, bg7,
                bg8, bg9, bg10, bg11, bg12, bg13, bg14, bg15,
                bg16, bg17, bg18, bg19, bg20, bg21, bg22, bg23,
                bg24, bg25, bg26, bg27, bg28, DUNGEON_1, DUNGEON_FLOOR, BLANK,
};

struct BgLayerSettings {
    public:
        BgLayerSettings();
        ~BgLayerSettings();

        unsigned int charblock;
        BACKGROUND screenblock;
        int v_offset;
        int h_offset;
};

main.cpp

#include "player.h"                 // Line 1
#include "BgLayerSettings.h"

player Player;
BgLayerSettings BACKGROUND_0;
BgLayerSettings BACKGROUND_1;
BgLayerSettings BACKGROUND_2;
BgLayerSettings BACKGROUND_3;

// Omitted unrelated code

本质上,我试图类中更改h_offset对象的变量BACKGROUND_2player

当我尝试对此进行编译时,会出现以下错误:

player.cpp: In member function 'void player::ScrollScreen()':
player.cpp:101:3: error: 'BACKGROUND_2' was not declared in this scope
make: *** [player.o] Error 1

无论我尝试什么,我都无法克服此错误。有人能为我阐明一下吗?

提前致谢。

瑞安·巴特利(Ryan Bartley)

它看起来不像Player.cpp,特别是这一行...

BACKGROUND_2.h_offset += x-((240/2)-8);

可以看到Background_2的实例。如果要在main.cpp中实例化它,则Player.cpp将无法在构建过程中看到它。您应该将要更改的任何背景作为参考传递到函数中,并从main.cpp中进行更改。像这样的东西...

void player::ScrollScreen( BgLayerSettings &bg ){       // Line 99
    if(x>((240/2)-8)){
        bg.h_offset += x-((240/2)-8);
    }
}

您的main.cpp将是这样的...

player1.ScrollScreen( BACKGROUND_2 );

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章