Дружественные функции c++

Я не могу заставить работать дружественную функцию( она в самом конце кода ). Точнее из нее не могу получить доступ к полям класса, с которым пытаюсь "подружиться" . Ниже я приведу только код, относящийся к вопросу:

#pragma once
#include <SDL2/SDL.h>
#include <SDL2/SDL_image.h>
#include"defs.h"

int max(int x, int y);
int min(int x, int y);

class Bullet;
class Enemy;
class Spaceship;



class Entity
{
public:
    Entity(const double& x, const  double& y);
    Entity(const double& x, const  double& y, const  double& dx, const double& dy);

    virtual void act() = 0;
    virtual void blit(SDL_Renderer*& renderer) = 0;

protected:
    double x;
    double y;
    double dx;
    double dy;
    friend void Enemy::calcSlope(Bullet*& bullet);
};

class Spaceship : public Entity {
public:
    Spaceship(const double& x,const double& y, const  int& health,const short int& side,const  int& reload, SDL_Texture*const &texture, SDL_Texture*const &bulletTexture);

    virtual void act() = 0;
    void blit(SDL_Renderer*& renderer);
    virtual void fire() = 0;
protected:
    int w;
    int h;

    int health;
    int reload;

    short int side;

    Spaceship* next;
    SDL_Texture* texture;
    SDL_Texture* bulletTexture;

    friend class Stage;
    friend void Enemy::calcSlope(Bullet*& bullet);

};

class Enemy : public Spaceship {
public:
    Enemy(const double& x, const double& y, const  int& health, const short int& side, const int& reload, Bullet*& bulletHead, Bullet*& bulletTail, Spaceship*& player, SDL_Texture* const& texture, SDL_Texture* const& bulletTexture);

    void act();
    void fire();
private:
    void calcSlope(Bullet*& bullet);

    Spaceship*& player;
    Bullet*& bulletHead, *& bulletTail;
};

class Player : public Spaceship {
public:
    Player(const double& x, const double& y, const  int& health, const short int& side, const int& reload, bool* keyboard, Bullet*& bulletHead, Bullet*& bulletTail, SDL_Texture*const &texture, SDL_Texture*const &bulletTexture);

    void act();
    void fire();
private:
    Bullet*& bulletHead, *& bulletTail;

    bool* keyboard;

};

class Bullet : public Entity {
public:
    Bullet(const double& x, const  double& y, const  double& dx, const  double& dy, short     int&side, SDL_Texture* const& texture);

    void act();
    void blit(SDL_Renderer*& renderer);
private:
    int w;
    int h;
    short int side;

    int health;
    SDL_Texture* texture;

    Bullet* next;


    friend void Player::fire();
    friend void Enemy::fire();
    friend class Stage;
};

inline void Enemy::calcSlope(Bullet*& bullet) 
{
    int x1 = player->x + (player->w / 2);//Не дает доступ к полям
    //            ^^^^         ^^^^
}

Ответы (1 шт):

Автор решения: AlexGlebe

Элементами класса в разделе protected: может пользоваться только наследник и только у себя. По указателю этот доступ уже запрещается.
Ваша попытка сделать дружественный метод была неудачна

friend void Enemy::calcSlope(Bullet*& bullet); // ошибка

так как вы объявили всего-лишь предварительно class Enemy ;, а до полного определения класса ещё не дошли. И компилятор не может дать пока разрешение на неизвестный метод.
Если у вас невозможно менять порядок объявления классов, то можно просто сделать дружественным весь класс, как тип.

friend class Enemy ;
→ Ссылка