C++ 在另一个对象中引用一个对象的当前状态

标签 c++ object pointers reference forward-declaration

我四处寻找这个问题的答案,并尝试了许多解决方案,包括前向声明、指针和引用。我确定我只是在某处使用了不正确的语法。在浪费了许多时间之后,我决定转向堆栈溢出。

我正在尝试编写我的第一个 CPP 应用程序,作为一种学习体验。现在我有一个 Player 和一个 Ball 对象。我的 Ball 对象必须能够访问我的 player 对象中的一些成员变量和方法。我一直无法弄清楚如何做到这一点。下面是我的代码的一个极其简化的版本。特别重要的代码我已经注释掉了。

播放状态.hpp

#ifndef PLAYSTATE_HPP
#define PLAYSTATE_HPP
#include "Player.hpp"
#include "Ball.hpp"

class Player;
class Ball;

class PlayState
{
public:
    PlayState();
    Player player;
    Ball ball;
 };
#endif

播放状态.cpp

#include "PlayState.hpp"

PlayState::PlayState() {
}

void PlayState::update() {

    if (sf::Keyboard::isKeyPressed(sf::Keyboard::W))
    {
        ball.checkCollision();
        player.move(1);
    }
    ball.update();
}

void PlayState::draw()
{
    m_game.screen.clear();
    m_game.screen.draw( player.getSprite() );
    m_game.screen.draw( ball.getSprite() );
    m_game.screen.display();
}

播放器.hpp

#ifndef PLAYER_HPP
#define PLAYER_HPP

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

    void create();
    void setRotation(float);
    void setPosition(float, float);
};
#endif

Player.cpp 不应该真的那么重要。

球.hpp

#ifndef BALL_HPP
#define BALL_HPP

class Player; // I don't think forward declaration is what I need???

class Ball
{
public:
    bool picked_up;
    bool throwing;

    Player *player; // this isn't working

    Ball();
    ~Ball();

    bool checkCollision();
};
#endif

球.cpp

#include "Ball.hpp"

Ball::Ball() {
    Ball::picked_up = false;
    Ball::throwing = false;
}

Ball::~Ball() {
}

bool Ball::checkCollision()
{
    float ball_position_x = Ball::getPosition().x;
    float ball_position_y = Ball::getPosition().y;

    // I need to access the player object here.
    float x_distance = abs(player.getPosition().x - ball_position_x);
    float y_distance = abs(player.getPosition().y - ball_position_y);

    bool is_colliding = (x_distance * 2 < (player.IMG_WIDTH + Ball::width)) && (y_distance * 2 < (player.IMG_HEIGHT + Ball::height));

    return is_colliding;
}

最佳答案

当你说 player 时,你是指与当前 ball 在同一 playstate 对象中的完全相同的 player 对象?如果是这样,您想首先设置该链接,它不能自动完成。

PlayState::PlayState() :ball(&player){ //pass pointer to ball of its player?
}

class Ball
...    
Ball(Player *myPlayer);
...

}

Ball::Ball(Player *myPlayer):player(myPlayer) {
...

// I need to access the player object here.
float x_distance = abs(player->getPosition().x - ball_position_x);

您还需要使用指针来使用播放器,因为它是指向播放器对象的指针。

您确实需要在 Ball 类之上对 Player 进行前向声明。上面的 Playstate 是不必要的。

此外,您的播放器似乎没有 GetPosition 函数,我假设这是您忘记包含在上面的公共(public)成员函数。

关于C++ 在另一个对象中引用一个对象的当前状态,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13737438/

相关文章:

java - 如何打印我的 Java 对象而不得到 "SomeType@2f92e0f4"?

C++ Shell 多管道不工作?

c++ - VTK Delete() 和数据删除

javascript - 尝试将原始对象推送到数组,并稍后找到其索引

C 函数和数组

c - 使用指针和 realloc 添加新记录

c++ - 使用 vector 指向基指针的指针数组

c++ - 我想打开一个文件,只获取该文件的一半文本,我不想获取所有内容。

c++ - boost spirit : Why does phrase_parse behave differently to parse when parsing eol?

php - 以正确的方式访问 stdClass 对象?