Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8961379
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T15:52:41+00:00 2026-06-15T15:52:41+00:00

I’ve searched high and low for an answer to this question and have attempted

  • 0

I’ve searched high and low for an answer to this question and have attempted many solutions including forward declaration, pointers, and references. I’m sure I’m just using incorrect syntax somewhere. After many wasted hours, I’ve decided to turn to stack overflow.

I am attempting to code one of my first CPP applications as a learning experience. Right now I have a Player and a Ball object. My Ball object must be able to access some of the member variables and methods in my player object. I have been unable to figure out how to do this. Below is an extremely simplified version of my code. I’ve commented the code that is particularly important.

PlayState.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

PlayState.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();
}

Player.hpp

#ifndef PLAYER_HPP
#define PLAYER_HPP

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

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

Player.cpp shouldn’t really be all that important to see.

Ball.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

Ball.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;
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T15:52:42+00:00Added an answer on June 15, 2026 at 3:52 pm

    When you say player, do you mean the exact same player that is in the same playstate object as the current ball object? If so you want to first set up that link, it cannot be done automatically.

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

    You also need to use a pointer to use the player since it is a pointer to a player object.

    You do need the forward declaration to Player above the Ball class. The one above Playstate is unnecessary.

    Also your player doesnt seem to have a GetPosition function, I am assuming it is a public member function you forgot to include above.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
This could be a duplicate question, but I have no idea what search terms
this is what i have right now Drawing an RSS feed into the php,
I have this code to decode numeric html entities to the UTF8 equivalent character.
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I don't have much knowledge about the IPv6 protocol, so sorry if the question
I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
link Im having trouble converting the html entites into html characters, (&# 8217;) i

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.