Martacus
Martacus

Reputation: 725

return function in another child class from the same parent returns nothing

Allright, so I have 3 classes which where 2 are a child class. The parent class is the Entity class which has a function that gets the coordinates of a sprite. If I run this function in the main() it works fine.

Then I have the Wolf class which needs a player passed in its constructor. In the wolf.cpp theres an update function which I run every tick and it needs to get the coords for the player.

My guess is I pass in the player wrong and it makes a copy or something. But I dont know how to do it properly and searching on google didnt really help right now. The best thing for me would be a straight answer. Here are the child classes. If you also need the entity class let me know.

Wolf.h

#pragma once
#include "Entity.h"
#include "Player.h"
class Wolf : public Entity{
public:
    Wolf(float speed, Player p);
    sf::Clock clock;
    sf::Vector2f playerCoords;
    Player player;
public:
    void update();
};

Wolf.cpp

#include <SFML/Window.hpp>
#include <SFML/Graphics.hpp>
#include "Wolf.h"
#include <iostream>
#include "math.h"

sf::Texture holdTexture; 
sf::Sprite holdSprite;


Wolf::Wolf(float speed, Player p) :
    Entity(holdSprite, speed),
    player(p)
{
    holdTexture.loadFromFile("Assets\\Wolf.png");
    sprite.setTexture(holdTexture);
}

Player.h

#pragma once
#include "Entity.h"
class Player : public Entity {
public:
    Player(sf::Sprite sprite, float speed);
    sf::Clock clock;
public:
    void update();
};

Player.cpp

#include "Player.h"

Player::Player(sf::Sprite sprite, float speed) :
    Entity(sprite, speed)
{}

Upvotes: 0

Views: 36

Answers (1)

πάντα ῥεῖ
πάντα ῥεῖ

Reputation: 1

You probably want to have a reference to Player:

class Wolf : public Entity{
public:
    Wolf(float speed, Player& p);
                         // ^
    sf::Clock clock;
    sf::Vector2f playerCoords;
    Player& player; // <<<<<<<<<<
       // ^
public:
    void update();
};

Wolf::Wolf(float speed, Player& p) :
                           // ^
    Entity(holdSprite, speed),
    player(p)
{
    // ...
}

This should fix your problems.

Upvotes: 1

Related Questions