C++ 类-C++;
我会尽力解释的 基本上,我是为GBA游戏编写这个程序的,我试图在类中更改struct实例的成员变量。这是代码,省略了不必要的部分: player.cppC++ 类-C++;,c++,class,struct,undefined,member,C++,Class,Struct,Undefined,Member,我会尽力解释的 基本上,我是为GBA游戏编写这个程序的,我试图在类中更改struct实例的成员变量。这是代码,省略了不必要的部分: player.cpp #include "player.h" // Line 1 #include "BgLayerSettings.h" player::player(){ x = 16; y = 16; health = 5; direction = LEFT; dead = false; }
#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.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;
};
本质上,我试图从player
类中更改对象BACKGROUND\u 2
的变量h\u offset
当我尝试编译此文件时,收到以下错误:
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
不管我怎么努力,我都无法克服这个错误。有人能帮我解释一下吗
提前感谢。它看起来不像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 );
那么
player.h
中的原型如何查找void screen()代码>在这种情况下?类似于void ScrollScreen(BgLayerSettings&b),如果您不想添加BgLayerSettings的include,您可以通过放置类BgLayerSettings向前声明它;超过你的玩家定义。已经为此辛劳了几个小时,终于修好了!非常感谢:)我自己也去过那里。
void player::ScrollScreen( BgLayerSettings &bg ){ // Line 99
if(x>((240/2)-8)){
bg.h_offset += x-((240/2)-8);
}
}
player1.ScrollScreen( BACKGROUND_2 );