Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/visual-studio/7.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ (SFML)继承问题(C+;+;)_C++_Visual Studio_Sfml - Fatal编程技术网

C++ (SFML)继承问题(C+;+;)

C++ (SFML)继承问题(C+;+;),c++,visual-studio,sfml,C++,Visual Studio,Sfml,因此,我正在使用SFML,并试图设置一个实体类和一个从中继承的播放器子类,但这是我第一次使用继承,我遇到了一些问题: 首先,我有一个AssetManager类,我从不同的来源拼凑而成,因为我还不太了解它们是如何工作的: AssetManager.h: class AssetManager { public: AssetManager(); static sf::Texture& LoadTexture(std::string const& path);

因此,我正在使用SFML,并试图设置一个实体类和一个从中继承的播放器子类,但这是我第一次使用继承,我遇到了一些问题: 首先,我有一个AssetManager类,我从不同的来源拼凑而成,因为我还不太了解它们是如何工作的:

AssetManager.h:

class AssetManager {
public:
    AssetManager();
    
    static sf::Texture& LoadTexture(std::string const& path);
    static sf::SoundBuffer& LoadSoundBuffer(std::string const& path);
    static sf::Font& LoadFont(std::string const& path);

private:
    std::map<std::string, sf::Texture> m_Textures;
    std::map<std::string, sf::SoundBuffer> m_SoundBuffers;
    std::map<std::string, sf::Font> m_Fonts;

    static AssetManager* sInstance;
};
AssetManager* AssetManager::sInstance = nullptr;

AssetManager::AssetManager() {
    assert(sInstance == nullptr);
    sInstance = this;
}

sf::Texture& AssetManager::LoadTexture(std::string const& path) {

    auto& texMap = sInstance->m_Textures;
    auto pairFound = texMap.find(path);

    if (pairFound != texMap.end()) {
        return pairFound->second;
    }
    else {
        auto& texture = texMap[path];
        texture.loadFromFile(path);
        return texture;
    }

}
class Sprite {
public:
    AssetManager manager;
    sf::Texture m_Texture;
    sf::Sprite m_Sprite;

    sf::Vector2f sprite_scale;
    sf::Vector2u original_size;
    sf::Vector2f texture_size;

    Sprite(std::string path,sf::IntRect rect,sf::Vector2f size);
    
};
Sprite::Sprite(std::string path, sf::IntRect rect, sf::Vector2f size) {
    m_Texture = sf::Texture(AssetManager::LoadTexture(path));
    m_Sprite.setTextureRect(rect);
    m_Sprite.setTexture(m_Texture);

    original_size = m_Texture.getSize();

    texture_size.x = static_cast<float>(original_size.x);
    texture_size.y = static_cast<float>(original_size.y);

    sprite_scale.x = size.x / texture_size.x;
    sprite_scale.y = size.y / texture_size.y;

    m_Sprite.setScale(sf::Vector2f(sprite_scale.x, sprite_scale.y));
    m_Sprite.setOrigin(sf::Vector2f(original_size.x / 2, original_size.y / 2));

}
class Entity {
public:
    Sprite entity_sprite;
    int health;
    float speed;
    bool collision = false;

    bool entity_collision(sf::Sprite entity2_sprite);// Entity.cpp only contains the declaration of this function so far so no need to post it.
    

};
class Player:public Entity {
    Player() {
        entity_sprite = Sprite("res/wildcard.png", { 0,0,36,63 }, { 36,63 });
    }
};
#include "AssetManager.h"

class Sprite{
public:
    sf::Sprite m_sprite;

    sf::Vector2f sprite_scale;
    sf::Vector2u original_size;
    sf::Vector2f texture_size;

    Sprite(){}
    sf::Sprite set_sprite(sf::Texture& tx, sf::IntRect rect, sf::Vector2f size);
};
#include "Sprite.h"

sf::Sprite Sprite::set_sprite(sf::Texture& tx, sf::IntRect rect, sf::Vector2f size) {

    sf::Sprite spr(tx);

    spr.setTextureRect(rect);

    original_size =tx.getSize();

    texture_size.x = static_cast<float>(original_size.x);
    texture_size.y = static_cast<float>(original_size.y);

    sprite_scale.x = size.x / texture_size.x;
    sprite_scale.y = size.y / texture_size.y;

    spr.setScale(sf::Vector2f(sprite_scale.x, sprite_scale.y));
    spr.setOrigin(sf::Vector2f(original_size.x / 2, original_size.y / 2));

    return spr;
}
#pragma once
#include "Sprite.h"
#include "collision.h"
#include "Timer.h"

class Entity {
public:
    Sprite spr;
    sf::Sprite entity_sprite;
    int health;
    float max_speed;
    sf::Vector2f speed;
    sf::Vector2f direction;
    float acceleration;
    bool collision = false;
    timer t;
    float acc_time;
};
#pragma once
#include "Entity.h"

class Player:public Entity {
public:
    Player();
    float acc_time = t.accumulate_time();
    void keyboard_controls();
    void mouse_controls(sf::Vector2f cursor);
};
#include "Player.h"
#include <math.h>


Player::Player() {
    
    speed = { 0,0 };
    acceleration = 2;
    max_speed = 500 + acceleration;
    entity_sprite = spr.set_sprite(AssetManager::LoadTexture("res/wildcard.png"), { 0,0,60,63 }, { 60,63 });
}
然后该类的一个对象包含在
Sprite
类中,这有助于为我声明Sprite

Sprite.h:

class AssetManager {
public:
    AssetManager();
    
    static sf::Texture& LoadTexture(std::string const& path);
    static sf::SoundBuffer& LoadSoundBuffer(std::string const& path);
    static sf::Font& LoadFont(std::string const& path);

private:
    std::map<std::string, sf::Texture> m_Textures;
    std::map<std::string, sf::SoundBuffer> m_SoundBuffers;
    std::map<std::string, sf::Font> m_Fonts;

    static AssetManager* sInstance;
};
AssetManager* AssetManager::sInstance = nullptr;

AssetManager::AssetManager() {
    assert(sInstance == nullptr);
    sInstance = this;
}

sf::Texture& AssetManager::LoadTexture(std::string const& path) {

    auto& texMap = sInstance->m_Textures;
    auto pairFound = texMap.find(path);

    if (pairFound != texMap.end()) {
        return pairFound->second;
    }
    else {
        auto& texture = texMap[path];
        texture.loadFromFile(path);
        return texture;
    }

}
class Sprite {
public:
    AssetManager manager;
    sf::Texture m_Texture;
    sf::Sprite m_Sprite;

    sf::Vector2f sprite_scale;
    sf::Vector2u original_size;
    sf::Vector2f texture_size;

    Sprite(std::string path,sf::IntRect rect,sf::Vector2f size);
    
};
Sprite::Sprite(std::string path, sf::IntRect rect, sf::Vector2f size) {
    m_Texture = sf::Texture(AssetManager::LoadTexture(path));
    m_Sprite.setTextureRect(rect);
    m_Sprite.setTexture(m_Texture);

    original_size = m_Texture.getSize();

    texture_size.x = static_cast<float>(original_size.x);
    texture_size.y = static_cast<float>(original_size.y);

    sprite_scale.x = size.x / texture_size.x;
    sprite_scale.y = size.y / texture_size.y;

    m_Sprite.setScale(sf::Vector2f(sprite_scale.x, sprite_scale.y));
    m_Sprite.setOrigin(sf::Vector2f(original_size.x / 2, original_size.y / 2));

}
class Entity {
public:
    Sprite entity_sprite;
    int health;
    float speed;
    bool collision = false;

    bool entity_collision(sf::Sprite entity2_sprite);// Entity.cpp only contains the declaration of this function so far so no need to post it.
    

};
class Player:public Entity {
    Player() {
        entity_sprite = Sprite("res/wildcard.png", { 0,0,36,63 }, { 36,63 });
    }
};
#include "AssetManager.h"

class Sprite{
public:
    sf::Sprite m_sprite;

    sf::Vector2f sprite_scale;
    sf::Vector2u original_size;
    sf::Vector2f texture_size;

    Sprite(){}
    sf::Sprite set_sprite(sf::Texture& tx, sf::IntRect rect, sf::Vector2f size);
};
#include "Sprite.h"

sf::Sprite Sprite::set_sprite(sf::Texture& tx, sf::IntRect rect, sf::Vector2f size) {

    sf::Sprite spr(tx);

    spr.setTextureRect(rect);

    original_size =tx.getSize();

    texture_size.x = static_cast<float>(original_size.x);
    texture_size.y = static_cast<float>(original_size.y);

    sprite_scale.x = size.x / texture_size.x;
    sprite_scale.y = size.y / texture_size.y;

    spr.setScale(sf::Vector2f(sprite_scale.x, sprite_scale.y));
    spr.setOrigin(sf::Vector2f(original_size.x / 2, original_size.y / 2));

    return spr;
}
#pragma once
#include "Sprite.h"
#include "collision.h"
#include "Timer.h"

class Entity {
public:
    Sprite spr;
    sf::Sprite entity_sprite;
    int health;
    float max_speed;
    sf::Vector2f speed;
    sf::Vector2f direction;
    float acceleration;
    bool collision = false;
    timer t;
    float acc_time;
};
#pragma once
#include "Entity.h"

class Player:public Entity {
public:
    Player();
    float acc_time = t.accumulate_time();
    void keyboard_controls();
    void mouse_controls(sf::Vector2f cursor);
};
#include "Player.h"
#include <math.h>


Player::Player() {
    
    speed = { 0,0 };
    acceleration = 2;
    max_speed = 500 + acceleration;
    entity_sprite = spr.set_sprite(AssetManager::LoadTexture("res/wildcard.png"), { 0,0,60,63 }, { 60,63 });
}
Sprite.cpp:

class AssetManager {
public:
    AssetManager();
    
    static sf::Texture& LoadTexture(std::string const& path);
    static sf::SoundBuffer& LoadSoundBuffer(std::string const& path);
    static sf::Font& LoadFont(std::string const& path);

private:
    std::map<std::string, sf::Texture> m_Textures;
    std::map<std::string, sf::SoundBuffer> m_SoundBuffers;
    std::map<std::string, sf::Font> m_Fonts;

    static AssetManager* sInstance;
};
AssetManager* AssetManager::sInstance = nullptr;

AssetManager::AssetManager() {
    assert(sInstance == nullptr);
    sInstance = this;
}

sf::Texture& AssetManager::LoadTexture(std::string const& path) {

    auto& texMap = sInstance->m_Textures;
    auto pairFound = texMap.find(path);

    if (pairFound != texMap.end()) {
        return pairFound->second;
    }
    else {
        auto& texture = texMap[path];
        texture.loadFromFile(path);
        return texture;
    }

}
class Sprite {
public:
    AssetManager manager;
    sf::Texture m_Texture;
    sf::Sprite m_Sprite;

    sf::Vector2f sprite_scale;
    sf::Vector2u original_size;
    sf::Vector2f texture_size;

    Sprite(std::string path,sf::IntRect rect,sf::Vector2f size);
    
};
Sprite::Sprite(std::string path, sf::IntRect rect, sf::Vector2f size) {
    m_Texture = sf::Texture(AssetManager::LoadTexture(path));
    m_Sprite.setTextureRect(rect);
    m_Sprite.setTexture(m_Texture);

    original_size = m_Texture.getSize();

    texture_size.x = static_cast<float>(original_size.x);
    texture_size.y = static_cast<float>(original_size.y);

    sprite_scale.x = size.x / texture_size.x;
    sprite_scale.y = size.y / texture_size.y;

    m_Sprite.setScale(sf::Vector2f(sprite_scale.x, sprite_scale.y));
    m_Sprite.setOrigin(sf::Vector2f(original_size.x / 2, original_size.y / 2));

}
class Entity {
public:
    Sprite entity_sprite;
    int health;
    float speed;
    bool collision = false;

    bool entity_collision(sf::Sprite entity2_sprite);// Entity.cpp only contains the declaration of this function so far so no need to post it.
    

};
class Player:public Entity {
    Player() {
        entity_sprite = Sprite("res/wildcard.png", { 0,0,36,63 }, { 36,63 });
    }
};
#include "AssetManager.h"

class Sprite{
public:
    sf::Sprite m_sprite;

    sf::Vector2f sprite_scale;
    sf::Vector2u original_size;
    sf::Vector2f texture_size;

    Sprite(){}
    sf::Sprite set_sprite(sf::Texture& tx, sf::IntRect rect, sf::Vector2f size);
};
#include "Sprite.h"

sf::Sprite Sprite::set_sprite(sf::Texture& tx, sf::IntRect rect, sf::Vector2f size) {

    sf::Sprite spr(tx);

    spr.setTextureRect(rect);

    original_size =tx.getSize();

    texture_size.x = static_cast<float>(original_size.x);
    texture_size.y = static_cast<float>(original_size.y);

    sprite_scale.x = size.x / texture_size.x;
    sprite_scale.y = size.y / texture_size.y;

    spr.setScale(sf::Vector2f(sprite_scale.x, sprite_scale.y));
    spr.setOrigin(sf::Vector2f(original_size.x / 2, original_size.y / 2));

    return spr;
}
#pragma once
#include "Sprite.h"
#include "collision.h"
#include "Timer.h"

class Entity {
public:
    Sprite spr;
    sf::Sprite entity_sprite;
    int health;
    float max_speed;
    sf::Vector2f speed;
    sf::Vector2f direction;
    float acceleration;
    bool collision = false;
    timer t;
    float acc_time;
};
#pragma once
#include "Entity.h"

class Player:public Entity {
public:
    Player();
    float acc_time = t.accumulate_time();
    void keyboard_controls();
    void mouse_controls(sf::Vector2f cursor);
};
#include "Player.h"
#include <math.h>


Player::Player() {
    
    speed = { 0,0 };
    acceleration = 2;
    max_speed = 500 + acceleration;
    entity_sprite = spr.set_sprite(AssetManager::LoadTexture("res/wildcard.png"), { 0,0,60,63 }, { 60,63 });
}
现在,出于我不理解的原因,我不能在声明
实体_sprite
对象时直接为其分配任何参数,而且我只能在没有参数的情况下声明它,尽管该类没有默认构造函数。 但是,我可以通过分配任务绕过它:
Entity-Entity_-sprite=Entity(“res/wildcard.png”,{0,0,36,63},{36,63})

但这不是主要问题,直接使用
实体
类并不是我想要做的,我试图编写
播放器
子类并使用它:

Player.h:

class AssetManager {
public:
    AssetManager();
    
    static sf::Texture& LoadTexture(std::string const& path);
    static sf::SoundBuffer& LoadSoundBuffer(std::string const& path);
    static sf::Font& LoadFont(std::string const& path);

private:
    std::map<std::string, sf::Texture> m_Textures;
    std::map<std::string, sf::SoundBuffer> m_SoundBuffers;
    std::map<std::string, sf::Font> m_Fonts;

    static AssetManager* sInstance;
};
AssetManager* AssetManager::sInstance = nullptr;

AssetManager::AssetManager() {
    assert(sInstance == nullptr);
    sInstance = this;
}

sf::Texture& AssetManager::LoadTexture(std::string const& path) {

    auto& texMap = sInstance->m_Textures;
    auto pairFound = texMap.find(path);

    if (pairFound != texMap.end()) {
        return pairFound->second;
    }
    else {
        auto& texture = texMap[path];
        texture.loadFromFile(path);
        return texture;
    }

}
class Sprite {
public:
    AssetManager manager;
    sf::Texture m_Texture;
    sf::Sprite m_Sprite;

    sf::Vector2f sprite_scale;
    sf::Vector2u original_size;
    sf::Vector2f texture_size;

    Sprite(std::string path,sf::IntRect rect,sf::Vector2f size);
    
};
Sprite::Sprite(std::string path, sf::IntRect rect, sf::Vector2f size) {
    m_Texture = sf::Texture(AssetManager::LoadTexture(path));
    m_Sprite.setTextureRect(rect);
    m_Sprite.setTexture(m_Texture);

    original_size = m_Texture.getSize();

    texture_size.x = static_cast<float>(original_size.x);
    texture_size.y = static_cast<float>(original_size.y);

    sprite_scale.x = size.x / texture_size.x;
    sprite_scale.y = size.y / texture_size.y;

    m_Sprite.setScale(sf::Vector2f(sprite_scale.x, sprite_scale.y));
    m_Sprite.setOrigin(sf::Vector2f(original_size.x / 2, original_size.y / 2));

}
class Entity {
public:
    Sprite entity_sprite;
    int health;
    float speed;
    bool collision = false;

    bool entity_collision(sf::Sprite entity2_sprite);// Entity.cpp only contains the declaration of this function so far so no need to post it.
    

};
class Player:public Entity {
    Player() {
        entity_sprite = Sprite("res/wildcard.png", { 0,0,36,63 }, { 36,63 });
    }
};
#include "AssetManager.h"

class Sprite{
public:
    sf::Sprite m_sprite;

    sf::Vector2f sprite_scale;
    sf::Vector2u original_size;
    sf::Vector2f texture_size;

    Sprite(){}
    sf::Sprite set_sprite(sf::Texture& tx, sf::IntRect rect, sf::Vector2f size);
};
#include "Sprite.h"

sf::Sprite Sprite::set_sprite(sf::Texture& tx, sf::IntRect rect, sf::Vector2f size) {

    sf::Sprite spr(tx);

    spr.setTextureRect(rect);

    original_size =tx.getSize();

    texture_size.x = static_cast<float>(original_size.x);
    texture_size.y = static_cast<float>(original_size.y);

    sprite_scale.x = size.x / texture_size.x;
    sprite_scale.y = size.y / texture_size.y;

    spr.setScale(sf::Vector2f(sprite_scale.x, sprite_scale.y));
    spr.setOrigin(sf::Vector2f(original_size.x / 2, original_size.y / 2));

    return spr;
}
#pragma once
#include "Sprite.h"
#include "collision.h"
#include "Timer.h"

class Entity {
public:
    Sprite spr;
    sf::Sprite entity_sprite;
    int health;
    float max_speed;
    sf::Vector2f speed;
    sf::Vector2f direction;
    float acceleration;
    bool collision = false;
    timer t;
    float acc_time;
};
#pragma once
#include "Entity.h"

class Player:public Entity {
public:
    Player();
    float acc_time = t.accumulate_time();
    void keyboard_controls();
    void mouse_controls(sf::Vector2f cursor);
};
#include "Player.h"
#include <math.h>


Player::Player() {
    
    speed = { 0,0 };
    acceleration = 2;
    max_speed = 500 + acceleration;
    entity_sprite = spr.set_sprite(AssetManager::LoadTexture("res/wildcard.png"), { 0,0,60,63 }, { 60,63 });
}
现在,我再次无法直接将参数分配给对象,因为类类型的对象的
调用没有适当的运算符()或将函数转换为指向函数类型的指针
(有趣的是,如果我返回到
实体
对象并在那里分配参数并假装错误不存在,
播放器
类产生的错误将变为
参数太多”和“初始值设定者太多”

这太令人困惑了

尽管如此,我还是能够再次使用赋值绕过它,与以前完全相同,只是这次我得到一个错误,说默认构造函数“Entity”不能被引用——它是一个已删除的函数。
,所以我返回到
Entity
类并添加一个空构造函数,如下所示:
Entity(){}
但是这个构造函数给了我另一个错误,说类“Sprite”不存在默认构造函数,即使实体类不是从Sprite类继承的,所以我进一步回到Sprite类,给它一个空构造函数:
Sprite(){}
,错误似乎消失了,直到我在
main.cpp
文件中声明了
Player
对象,并尝试编译并获得指向
AssetManager.cpp
中以下行的调试错误:
断言(sInstance==nullptr);


这么简单的任务有这么多问题,我该如何摆脱它?

好的,在咨询了SFML论坛后,我将代码重构为以下内容:

Sprite.h:

class AssetManager {
public:
    AssetManager();
    
    static sf::Texture& LoadTexture(std::string const& path);
    static sf::SoundBuffer& LoadSoundBuffer(std::string const& path);
    static sf::Font& LoadFont(std::string const& path);

private:
    std::map<std::string, sf::Texture> m_Textures;
    std::map<std::string, sf::SoundBuffer> m_SoundBuffers;
    std::map<std::string, sf::Font> m_Fonts;

    static AssetManager* sInstance;
};
AssetManager* AssetManager::sInstance = nullptr;

AssetManager::AssetManager() {
    assert(sInstance == nullptr);
    sInstance = this;
}

sf::Texture& AssetManager::LoadTexture(std::string const& path) {

    auto& texMap = sInstance->m_Textures;
    auto pairFound = texMap.find(path);

    if (pairFound != texMap.end()) {
        return pairFound->second;
    }
    else {
        auto& texture = texMap[path];
        texture.loadFromFile(path);
        return texture;
    }

}
class Sprite {
public:
    AssetManager manager;
    sf::Texture m_Texture;
    sf::Sprite m_Sprite;

    sf::Vector2f sprite_scale;
    sf::Vector2u original_size;
    sf::Vector2f texture_size;

    Sprite(std::string path,sf::IntRect rect,sf::Vector2f size);
    
};
Sprite::Sprite(std::string path, sf::IntRect rect, sf::Vector2f size) {
    m_Texture = sf::Texture(AssetManager::LoadTexture(path));
    m_Sprite.setTextureRect(rect);
    m_Sprite.setTexture(m_Texture);

    original_size = m_Texture.getSize();

    texture_size.x = static_cast<float>(original_size.x);
    texture_size.y = static_cast<float>(original_size.y);

    sprite_scale.x = size.x / texture_size.x;
    sprite_scale.y = size.y / texture_size.y;

    m_Sprite.setScale(sf::Vector2f(sprite_scale.x, sprite_scale.y));
    m_Sprite.setOrigin(sf::Vector2f(original_size.x / 2, original_size.y / 2));

}
class Entity {
public:
    Sprite entity_sprite;
    int health;
    float speed;
    bool collision = false;

    bool entity_collision(sf::Sprite entity2_sprite);// Entity.cpp only contains the declaration of this function so far so no need to post it.
    

};
class Player:public Entity {
    Player() {
        entity_sprite = Sprite("res/wildcard.png", { 0,0,36,63 }, { 36,63 });
    }
};
#include "AssetManager.h"

class Sprite{
public:
    sf::Sprite m_sprite;

    sf::Vector2f sprite_scale;
    sf::Vector2u original_size;
    sf::Vector2f texture_size;

    Sprite(){}
    sf::Sprite set_sprite(sf::Texture& tx, sf::IntRect rect, sf::Vector2f size);
};
#include "Sprite.h"

sf::Sprite Sprite::set_sprite(sf::Texture& tx, sf::IntRect rect, sf::Vector2f size) {

    sf::Sprite spr(tx);

    spr.setTextureRect(rect);

    original_size =tx.getSize();

    texture_size.x = static_cast<float>(original_size.x);
    texture_size.y = static_cast<float>(original_size.y);

    sprite_scale.x = size.x / texture_size.x;
    sprite_scale.y = size.y / texture_size.y;

    spr.setScale(sf::Vector2f(sprite_scale.x, sprite_scale.y));
    spr.setOrigin(sf::Vector2f(original_size.x / 2, original_size.y / 2));

    return spr;
}
#pragma once
#include "Sprite.h"
#include "collision.h"
#include "Timer.h"

class Entity {
public:
    Sprite spr;
    sf::Sprite entity_sprite;
    int health;
    float max_speed;
    sf::Vector2f speed;
    sf::Vector2f direction;
    float acceleration;
    bool collision = false;
    timer t;
    float acc_time;
};
#pragma once
#include "Entity.h"

class Player:public Entity {
public:
    Player();
    float acc_time = t.accumulate_time();
    void keyboard_controls();
    void mouse_controls(sf::Vector2f cursor);
};
#include "Player.h"
#include <math.h>


Player::Player() {
    
    speed = { 0,0 };
    acceleration = 2;
    max_speed = 500 + acceleration;
    entity_sprite = spr.set_sprite(AssetManager::LoadTexture("res/wildcard.png"), { 0,0,60,63 }, { 60,63 });
}
Sprite.cpp:

class AssetManager {
public:
    AssetManager();
    
    static sf::Texture& LoadTexture(std::string const& path);
    static sf::SoundBuffer& LoadSoundBuffer(std::string const& path);
    static sf::Font& LoadFont(std::string const& path);

private:
    std::map<std::string, sf::Texture> m_Textures;
    std::map<std::string, sf::SoundBuffer> m_SoundBuffers;
    std::map<std::string, sf::Font> m_Fonts;

    static AssetManager* sInstance;
};
AssetManager* AssetManager::sInstance = nullptr;

AssetManager::AssetManager() {
    assert(sInstance == nullptr);
    sInstance = this;
}

sf::Texture& AssetManager::LoadTexture(std::string const& path) {

    auto& texMap = sInstance->m_Textures;
    auto pairFound = texMap.find(path);

    if (pairFound != texMap.end()) {
        return pairFound->second;
    }
    else {
        auto& texture = texMap[path];
        texture.loadFromFile(path);
        return texture;
    }

}
class Sprite {
public:
    AssetManager manager;
    sf::Texture m_Texture;
    sf::Sprite m_Sprite;

    sf::Vector2f sprite_scale;
    sf::Vector2u original_size;
    sf::Vector2f texture_size;

    Sprite(std::string path,sf::IntRect rect,sf::Vector2f size);
    
};
Sprite::Sprite(std::string path, sf::IntRect rect, sf::Vector2f size) {
    m_Texture = sf::Texture(AssetManager::LoadTexture(path));
    m_Sprite.setTextureRect(rect);
    m_Sprite.setTexture(m_Texture);

    original_size = m_Texture.getSize();

    texture_size.x = static_cast<float>(original_size.x);
    texture_size.y = static_cast<float>(original_size.y);

    sprite_scale.x = size.x / texture_size.x;
    sprite_scale.y = size.y / texture_size.y;

    m_Sprite.setScale(sf::Vector2f(sprite_scale.x, sprite_scale.y));
    m_Sprite.setOrigin(sf::Vector2f(original_size.x / 2, original_size.y / 2));

}
class Entity {
public:
    Sprite entity_sprite;
    int health;
    float speed;
    bool collision = false;

    bool entity_collision(sf::Sprite entity2_sprite);// Entity.cpp only contains the declaration of this function so far so no need to post it.
    

};
class Player:public Entity {
    Player() {
        entity_sprite = Sprite("res/wildcard.png", { 0,0,36,63 }, { 36,63 });
    }
};
#include "AssetManager.h"

class Sprite{
public:
    sf::Sprite m_sprite;

    sf::Vector2f sprite_scale;
    sf::Vector2u original_size;
    sf::Vector2f texture_size;

    Sprite(){}
    sf::Sprite set_sprite(sf::Texture& tx, sf::IntRect rect, sf::Vector2f size);
};
#include "Sprite.h"

sf::Sprite Sprite::set_sprite(sf::Texture& tx, sf::IntRect rect, sf::Vector2f size) {

    sf::Sprite spr(tx);

    spr.setTextureRect(rect);

    original_size =tx.getSize();

    texture_size.x = static_cast<float>(original_size.x);
    texture_size.y = static_cast<float>(original_size.y);

    sprite_scale.x = size.x / texture_size.x;
    sprite_scale.y = size.y / texture_size.y;

    spr.setScale(sf::Vector2f(sprite_scale.x, sprite_scale.y));
    spr.setOrigin(sf::Vector2f(original_size.x / 2, original_size.y / 2));

    return spr;
}
#pragma once
#include "Sprite.h"
#include "collision.h"
#include "Timer.h"

class Entity {
public:
    Sprite spr;
    sf::Sprite entity_sprite;
    int health;
    float max_speed;
    sf::Vector2f speed;
    sf::Vector2f direction;
    float acceleration;
    bool collision = false;
    timer t;
    float acc_time;
};
#pragma once
#include "Entity.h"

class Player:public Entity {
public:
    Player();
    float acc_time = t.accumulate_time();
    void keyboard_controls();
    void mouse_controls(sf::Vector2f cursor);
};
#include "Player.h"
#include <math.h>


Player::Player() {
    
    speed = { 0,0 };
    acceleration = 2;
    max_speed = 500 + acceleration;
    entity_sprite = spr.set_sprite(AssetManager::LoadTexture("res/wildcard.png"), { 0,0,60,63 }, { 60,63 });
}
Player.h:

class AssetManager {
public:
    AssetManager();
    
    static sf::Texture& LoadTexture(std::string const& path);
    static sf::SoundBuffer& LoadSoundBuffer(std::string const& path);
    static sf::Font& LoadFont(std::string const& path);

private:
    std::map<std::string, sf::Texture> m_Textures;
    std::map<std::string, sf::SoundBuffer> m_SoundBuffers;
    std::map<std::string, sf::Font> m_Fonts;

    static AssetManager* sInstance;
};
AssetManager* AssetManager::sInstance = nullptr;

AssetManager::AssetManager() {
    assert(sInstance == nullptr);
    sInstance = this;
}

sf::Texture& AssetManager::LoadTexture(std::string const& path) {

    auto& texMap = sInstance->m_Textures;
    auto pairFound = texMap.find(path);

    if (pairFound != texMap.end()) {
        return pairFound->second;
    }
    else {
        auto& texture = texMap[path];
        texture.loadFromFile(path);
        return texture;
    }

}
class Sprite {
public:
    AssetManager manager;
    sf::Texture m_Texture;
    sf::Sprite m_Sprite;

    sf::Vector2f sprite_scale;
    sf::Vector2u original_size;
    sf::Vector2f texture_size;

    Sprite(std::string path,sf::IntRect rect,sf::Vector2f size);
    
};
Sprite::Sprite(std::string path, sf::IntRect rect, sf::Vector2f size) {
    m_Texture = sf::Texture(AssetManager::LoadTexture(path));
    m_Sprite.setTextureRect(rect);
    m_Sprite.setTexture(m_Texture);

    original_size = m_Texture.getSize();

    texture_size.x = static_cast<float>(original_size.x);
    texture_size.y = static_cast<float>(original_size.y);

    sprite_scale.x = size.x / texture_size.x;
    sprite_scale.y = size.y / texture_size.y;

    m_Sprite.setScale(sf::Vector2f(sprite_scale.x, sprite_scale.y));
    m_Sprite.setOrigin(sf::Vector2f(original_size.x / 2, original_size.y / 2));

}
class Entity {
public:
    Sprite entity_sprite;
    int health;
    float speed;
    bool collision = false;

    bool entity_collision(sf::Sprite entity2_sprite);// Entity.cpp only contains the declaration of this function so far so no need to post it.
    

};
class Player:public Entity {
    Player() {
        entity_sprite = Sprite("res/wildcard.png", { 0,0,36,63 }, { 36,63 });
    }
};
#include "AssetManager.h"

class Sprite{
public:
    sf::Sprite m_sprite;

    sf::Vector2f sprite_scale;
    sf::Vector2u original_size;
    sf::Vector2f texture_size;

    Sprite(){}
    sf::Sprite set_sprite(sf::Texture& tx, sf::IntRect rect, sf::Vector2f size);
};
#include "Sprite.h"

sf::Sprite Sprite::set_sprite(sf::Texture& tx, sf::IntRect rect, sf::Vector2f size) {

    sf::Sprite spr(tx);

    spr.setTextureRect(rect);

    original_size =tx.getSize();

    texture_size.x = static_cast<float>(original_size.x);
    texture_size.y = static_cast<float>(original_size.y);

    sprite_scale.x = size.x / texture_size.x;
    sprite_scale.y = size.y / texture_size.y;

    spr.setScale(sf::Vector2f(sprite_scale.x, sprite_scale.y));
    spr.setOrigin(sf::Vector2f(original_size.x / 2, original_size.y / 2));

    return spr;
}
#pragma once
#include "Sprite.h"
#include "collision.h"
#include "Timer.h"

class Entity {
public:
    Sprite spr;
    sf::Sprite entity_sprite;
    int health;
    float max_speed;
    sf::Vector2f speed;
    sf::Vector2f direction;
    float acceleration;
    bool collision = false;
    timer t;
    float acc_time;
};
#pragma once
#include "Entity.h"

class Player:public Entity {
public:
    Player();
    float acc_time = t.accumulate_time();
    void keyboard_controls();
    void mouse_controls(sf::Vector2f cursor);
};
#include "Player.h"
#include <math.h>


Player::Player() {
    
    speed = { 0,0 };
    acceleration = 2;
    max_speed = 500 + acceleration;
    entity_sprite = spr.set_sprite(AssetManager::LoadTexture("res/wildcard.png"), { 0,0,60,63 }, { 60,63 });
}
Player.cpp:

class AssetManager {
public:
    AssetManager();
    
    static sf::Texture& LoadTexture(std::string const& path);
    static sf::SoundBuffer& LoadSoundBuffer(std::string const& path);
    static sf::Font& LoadFont(std::string const& path);

private:
    std::map<std::string, sf::Texture> m_Textures;
    std::map<std::string, sf::SoundBuffer> m_SoundBuffers;
    std::map<std::string, sf::Font> m_Fonts;

    static AssetManager* sInstance;
};
AssetManager* AssetManager::sInstance = nullptr;

AssetManager::AssetManager() {
    assert(sInstance == nullptr);
    sInstance = this;
}

sf::Texture& AssetManager::LoadTexture(std::string const& path) {

    auto& texMap = sInstance->m_Textures;
    auto pairFound = texMap.find(path);

    if (pairFound != texMap.end()) {
        return pairFound->second;
    }
    else {
        auto& texture = texMap[path];
        texture.loadFromFile(path);
        return texture;
    }

}
class Sprite {
public:
    AssetManager manager;
    sf::Texture m_Texture;
    sf::Sprite m_Sprite;

    sf::Vector2f sprite_scale;
    sf::Vector2u original_size;
    sf::Vector2f texture_size;

    Sprite(std::string path,sf::IntRect rect,sf::Vector2f size);
    
};
Sprite::Sprite(std::string path, sf::IntRect rect, sf::Vector2f size) {
    m_Texture = sf::Texture(AssetManager::LoadTexture(path));
    m_Sprite.setTextureRect(rect);
    m_Sprite.setTexture(m_Texture);

    original_size = m_Texture.getSize();

    texture_size.x = static_cast<float>(original_size.x);
    texture_size.y = static_cast<float>(original_size.y);

    sprite_scale.x = size.x / texture_size.x;
    sprite_scale.y = size.y / texture_size.y;

    m_Sprite.setScale(sf::Vector2f(sprite_scale.x, sprite_scale.y));
    m_Sprite.setOrigin(sf::Vector2f(original_size.x / 2, original_size.y / 2));

}
class Entity {
public:
    Sprite entity_sprite;
    int health;
    float speed;
    bool collision = false;

    bool entity_collision(sf::Sprite entity2_sprite);// Entity.cpp only contains the declaration of this function so far so no need to post it.
    

};
class Player:public Entity {
    Player() {
        entity_sprite = Sprite("res/wildcard.png", { 0,0,36,63 }, { 36,63 });
    }
};
#include "AssetManager.h"

class Sprite{
public:
    sf::Sprite m_sprite;

    sf::Vector2f sprite_scale;
    sf::Vector2u original_size;
    sf::Vector2f texture_size;

    Sprite(){}
    sf::Sprite set_sprite(sf::Texture& tx, sf::IntRect rect, sf::Vector2f size);
};
#include "Sprite.h"

sf::Sprite Sprite::set_sprite(sf::Texture& tx, sf::IntRect rect, sf::Vector2f size) {

    sf::Sprite spr(tx);

    spr.setTextureRect(rect);

    original_size =tx.getSize();

    texture_size.x = static_cast<float>(original_size.x);
    texture_size.y = static_cast<float>(original_size.y);

    sprite_scale.x = size.x / texture_size.x;
    sprite_scale.y = size.y / texture_size.y;

    spr.setScale(sf::Vector2f(sprite_scale.x, sprite_scale.y));
    spr.setOrigin(sf::Vector2f(original_size.x / 2, original_size.y / 2));

    return spr;
}
#pragma once
#include "Sprite.h"
#include "collision.h"
#include "Timer.h"

class Entity {
public:
    Sprite spr;
    sf::Sprite entity_sprite;
    int health;
    float max_speed;
    sf::Vector2f speed;
    sf::Vector2f direction;
    float acceleration;
    bool collision = false;
    timer t;
    float acc_time;
};
#pragma once
#include "Entity.h"

class Player:public Entity {
public:
    Player();
    float acc_time = t.accumulate_time();
    void keyboard_controls();
    void mouse_controls(sf::Vector2f cursor);
};
#include "Player.h"
#include <math.h>


Player::Player() {
    
    speed = { 0,0 };
    acceleration = 2;
    max_speed = 500 + acceleration;
    entity_sprite = spr.set_sprite(AssetManager::LoadTexture("res/wildcard.png"), { 0,0,60,63 }, { 60,63 });
}
#包括“Player.h”
#包括
Player::Player(){
速度={0,0};
加速度=2;
最大速度=500+加速度;
实体_sprite=spr.set_sprite(AssetManager::LoadTexture(“res/wildcard.png”),{0,0,60,63},{60,63});
}

简而言之,
Sprite
class'构造函数被一个具有相同确切角色的方法所取代,这样我就可以简单地声明一个
Sprite
对象,在
实体
类中没有参数,并且我不会对派生的
播放器
类产生任何问题,因为我不会被要求创建默认的构造函数Sprite和Entity类的ors。

即使Entity类不是从Sprite类继承的,也不必继承。它有一个类型为
Sprite
的字段,因此它需要知道如何构造这样的字段。添加构造函数没有解决任何问题,因为编译器正在删除构造函数,因为下面的问题与<代码> SpRITE < /代码> -所以你必须确保所有类型的值都是可构造的。你能提供一个产生错误的代码的例子吗?在最后两段中你所说的不太清楚。BTW,考虑删除警告的截屏或者移动它到一个不同的问题,因为它不是RE。与此问题相关,使阅读更加混乱。谢谢you@Miguel明白了,我删除了问题的这一部分,并添加了我自己的答案,因为我就这个问题咨询了SFML论坛。en.SFML-dev.org/forums/index.php?topic=27616.0