return 另一个 child class 中的函数来自同一个 parent return 什么都没有
return function in another child class from the same parent returns nothing
好吧,所以我有 3 个 classes,其中 2 个是 child class。 parent class 是实体 class ,它具有获取精灵坐标的功能。如果我 运行 这个函数在 main() 中它工作正常。
然后我有 Wolf class,它需要在其构造函数中传递一个玩家。在 wolf.cpp 中有一个更新函数,我 运行 每个刻度都需要它来获取玩家的坐标。
我的猜测是我错误地传递了播放器,它制作了一个副本或其他东西。但我不知道如何正确地做到这一点,并且在 google 上搜索现在并没有真正的帮助。对我来说最好的事情就是直接回答。这是 child classes。 如果您还需要实体 class 让我知道。
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)
{}
您可能想引用 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)
{
// ...
}
这应该可以解决您的问题。
好吧,所以我有 3 个 classes,其中 2 个是 child class。 parent class 是实体 class ,它具有获取精灵坐标的功能。如果我 运行 这个函数在 main() 中它工作正常。
然后我有 Wolf class,它需要在其构造函数中传递一个玩家。在 wolf.cpp 中有一个更新函数,我 运行 每个刻度都需要它来获取玩家的坐标。
我的猜测是我错误地传递了播放器,它制作了一个副本或其他东西。但我不知道如何正确地做到这一点,并且在 google 上搜索现在并没有真正的帮助。对我来说最好的事情就是直接回答。这是 child classes。 如果您还需要实体 class 让我知道。
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)
{}
您可能想引用 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)
{
// ...
}
这应该可以解决您的问题。