vendredi 26 août 2016

call a member class in another class

I have a class called Ball and a class Player.Inside my Ball class I create two pointers of type Player and I have two member methods so when I change the ball I can point to different players(current and previous). So I know that if I want to call the member methods of Ball inside a method in class Player they have to be static (and I also think that static methods operate with static variables too) but I seem to have huge difficulty and whatever I try I get a different error.

Here follows Player.h

#ifndef PLAYER_H
#define PLAYER_H
#include <iostream>
#include "Player.h"

using namespace std;

class Player
{
    public:
        //constructor of players
        Player(const string &,int,int,int,int); //name,num,x,y,target_line

        //setters and getters
        //player's movements and functions
        void pass(Player*);

    private:
        string Name;
        int Num;
        int X;//0-5
        int Y;//0-9
        int Movement_line;
        int Target_line;

};

#endif // PLAYER_H

Here follows Player.cpp in which in method pass the problem I described occurs.

#include "Player.h"
#include <stdio.h>
#include <iostream>
#include "Team.h"
#include "Ball.h"
#include <cstdlib>

using namespace std;


Player::Player(const string &name,int num,int x,int y,int target_line)
:   Name(name),
    Num(num),
    X(x),
    Y(y),
    Movement_line(y),
    Target_line(target_line)
{
}

//setters and getters and other member functions

void Player::pass(Player* next){
    changeCurrentToPrevious();
    changeNextToCurrent(next);
}

Here follows Ball.h in which I define two pointers of type Player.

#ifndef BALL_H
#define BALL_H
#include "Player.h"

class Ball
{
    public:
        Ball();
        Player* current;
        Player* previous;

        /*void setX_ball(int);
        int getX_ball() const;
        void setY_ball(int);
        int getY_ball() const;
        void assign();*/
        static void changeCurrentToPrevious();
        static void changeNextToCurrent(Player*);


    private:
        int X_ball;
        int Y_ball;
};

#endif // BALL_H

Here follows Ball.cpp in which in the constructor the pointers are set to null.

#include "Ball.h"
#include "Team.h"
#include "Player.h"

Ball::Ball()
:   X_ball(2),
    Y_ball(5)
{
    current = NULL;
    previous = NULL;

}

static void Ball::changeCurrentToPrevious(){//when pass occurs
    previous = current ;
}

static void Ball::changeNextToCurrent(Player* next){
    current = next;
}

Aucun commentaire:

Enregistrer un commentaire