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

Martacus

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)
{}
πάντα ῥεῖ

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.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

In general, is there a there a way to access a Child class from another child class when they both share the same parent class?

Calling different child class function from the same parent invocation

Return function returns nothing

Return child class from parent class

Copying the data of a child class of a parent to another class with same parent

Return from parent function in a child function

go return child type from parent function

XPath: how to navigate from a child to another child of the same parent

Refresh Parent function widget from child class

oop call parent function from child class

How to make a return from a child function cause a return to the parent function?

Accessing object from another class of the same parent

Child class: *this = function_which_returns_parent_object(), is it possible?

Make parent class return a child from class factory

Return type of child class from parent class method

C# return parent class value from child class?

How to call parent class function from child class function in Flutter

create child class from parent class, same arguments

Python - Child Class to call a function from another Child Class

Access parent class instance variable from child class function

Flutter calling child class function from parent class

A function in child class inherited from a parent class uses that ones variables?

Virtual function from child class calls parent function once

Child class attributes point to the same object reference from parent

Calling child variables from another generic class which generic is parent

Get return value from function in another Class

Can you return another instance of the same class from the constructor function (Singleton Pattern)?

exit from parent function when child return false

How to return child class's object when function return type is parent class?

TOP Ranking

  1. 1

    Failed to listen on localhost:8000 (reason: Cannot assign requested address)

  2. 2

    How to import an asset in swift using Bundle.main.path() in a react-native native module

  3. 3

    Loopback Error: connect ECONNREFUSED 127.0.0.1:3306 (MAMP)

  4. 4

    pump.io port in URL

  5. 5

    Spring Boot JPA PostgreSQL Web App - Internal Authentication Error

  6. 6

    BigQuery - concatenate ignoring NULL

  7. 7

    ngClass error (Can't bind ngClass since it isn't a known property of div) in Angular 11.0.3

  8. 8

    Do Idle Snowflake Connections Use Cloud Services Credits?

  9. 9

    maven-jaxb2-plugin cannot generate classes due to two declarations cause a collision in ObjectFactory class

  10. 10

    Compiler error CS0246 (type or namespace not found) on using Ninject in ASP.NET vNext

  11. 11

    Can't pre-populate phone number and message body in SMS link on iPhones when SMS app is not running in the background

  12. 12

    Generate random UUIDv4 with Elm

  13. 13

    Jquery different data trapped from direct mousedown event and simulation via $(this).trigger('mousedown');

  14. 14

    Is it possible to Redo commits removed by GitHub Desktop's Undo on a Mac?

  15. 15

    flutter: dropdown item programmatically unselect problem

  16. 16

    Change dd-mm-yyyy date format of dataframe date column to yyyy-mm-dd

  17. 17

    EXCEL: Find sum of values in one column with criteria from other column

  18. 18

    Pandas - check if dataframe has negative value in any column

  19. 19

    How to use merge windows unallocated space into Ubuntu using GParted?

  20. 20

    Make a B+ Tree concurrent thread safe

  21. 21

    ggplotly no applicable method for 'plotly_build' applied to an object of class "NULL" if statements

HotTag

Archive