如何在其他类方法中引用类属性?

发布于 2025-02-11 01:49:28 字数 676 浏览 0 评论 0原文

我正在尝试创建一个ASCII的小游戏,可以在其中绕过杀死敌人等。但是我是C ++的新手,如果玩家位置在某个点,我想做一些事情。

下面是代码的更简单版本,也是问题的图片:

#include <iostream>
using namespace std;

struct Game
{
    bool bGameOver = false;
    int iWidth = 20;
    int iHeight = 40;
    void Draw() {
        if (player.x == 5)
        {
            cout << "Hello"
        }
    }

};

struct Player
{
    bool bGameOver = false;
    int x = 0;
    int y = 0;

};

void Setup()
{

}

int main()
{
    Game game;
    Player player;
    while (!game.bGameOver)
    {
        Setup();
    }
}

错误的图片

I'm trying to create a little ascii game where I can run around kill enemies etc. However I'm new to C++ and I would like to do something if the players location is at a certain point.

Below is a simpler version of the code and a picture of the problem:

#include <iostream>
using namespace std;

struct Game
{
    bool bGameOver = false;
    int iWidth = 20;
    int iHeight = 40;
    void Draw() {
        if (player.x == 5)
        {
            cout << "Hello"
        }
    }

};

struct Player
{
    bool bGameOver = false;
    int x = 0;
    int y = 0;

};

void Setup()
{

}

int main()
{
    Game game;
    Player player;
    while (!game.bGameOver)
    {
        Setup();
    }
}

Picture of the error

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

一场春暖 2025-02-18 01:49:28

变量player在函数main中是本地的,因此在尝试在game :: draw中使用它的地方不可见。

一种解决方案可能是使player成为全局变量。您需要切换结构的顺序:

struct Player
{
    bool bGameOver = false;
    int x = 0;
    int y = 0;

};

Player player;

struct Game
{
    bool bGameOver = false;
    int iWidth = 20;
    int iHeight = 40;
    void Draw() {
        if (player.x == 5)
        {
            cout << "Hello"
        }
    }

};

但是我更喜欢建模物品,以便game“具有” player。因此,制作playergame的成员:(

struct Player
{
    bool bGameOver = false;
    int x = 0;
    int y = 0;

};

struct Game
{
    Player player;
    bool bGameOver = false;
    int iWidth = 20;
    int iHeight = 40;
    void Draw() {
        if (player.x == 5)
        {
            cout << "Hello"
        }
    }

};

一边:您可能不需要两个不同的值称为bgameover,因为将它们保持在同步中将是额外的工作。

The variable player is local in function main, so it's not visible where you tried to use it in Game::Draw.

One solution could be to make player a global variable. You'll need to switch the order of the structs:

struct Player
{
    bool bGameOver = false;
    int x = 0;
    int y = 0;

};

Player player;

struct Game
{
    bool bGameOver = false;
    int iWidth = 20;
    int iHeight = 40;
    void Draw() {
        if (player.x == 5)
        {
            cout << "Hello"
        }
    }

};

But I'd prefer to instead model things so a Game "has a" Player. So make Player a member of the Game:

struct Player
{
    bool bGameOver = false;
    int x = 0;
    int y = 0;

};

struct Game
{
    Player player;
    bool bGameOver = false;
    int iWidth = 20;
    int iHeight = 40;
    void Draw() {
        if (player.x == 5)
        {
            cout << "Hello"
        }
    }

};

(Aside: You probably don't want two different values called bGameOver, since keeping them in sync would be extra work. It sounds more like a game property than a player property to me.)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文