Question

I'll try and explain this the best I can...

Basically, I'm writing this program for a GBA game and I'm trying to change a member variable of a struct instance from within a class. This is the code, omitting unnecessary parts:

player.cpp

#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);
    }
}

player.h

#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

Essentially I'm trying to change the variable h_offset of the object BACKGROUND_2 from within the player class.

When I try to compile this, I receive this error:

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

No matter what I try, I cannot get past this error. Anyone able to shed some light on this for me?

Thanks in advance.

Was it helpful?

Solution

It doesn't look like Player.cpp, specifically this line...

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

can see the instance of BACKGROUND_2. If you are instantiating it in main.cpp then there'd be no way for Player.cpp to see that during the build. You should pass whatever background you want to change into the function as a reference and change it from the main.cpp. Something like this instead...

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

Your main.cpp would be something like this...

player1.ScrollScreen( BACKGROUND_2 );
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top