删除的函数,标准::p空气<标准::字符串,资产>引用

Deleted function, std::pair<std::string, Asset> reference

本文关键字:标准 引用 字符串 资产 gt 空气 函数 删除 lt      更新时间:2023-10-16

我有以下类:

class AssetManager
{
public:
    AssetManager();
    AssetManager(std::vector<const char *> sources);
    ~AssetManager();
    bool addSource(const char *file);
    std::vector<char> getAsset(const char *name);
private:
    class Asset
    {
    public:
        Asset(const char *name, std::size_t size, std::size_t location, std::ifstream &inputStream);
        ~Asset();
        const char *getName();
        std::size_t getSize();
        std::size_t getLocation();
        std::ifstream &getInputStream();
        //makes the name 16 bytes and filled with 0's
        static const char *makeName(const char *name);
    private:
        char name_[ASSET_NAME_LENGTH];
        std::size_t size_;
        std::size_t location_;
        std::ifstream &inputStream_;
    };
    Asset *findAssetByName(std::string &name, std::size_t start, std::size_t end);
    std::vector<std::pair<std::string, Asset>> sortedNames_;
    std::vector<std::ifstream> inputStreams_;
};

导致问题的代码部分:

AssetManager::AssetManager(std::vector<const char*> sources) : inputStreams_(sources.size())
{
    //some code....
    std::sort(sortedNames_.begin(), sortedNames_.end(),
        [](std::pair<std::string, Asset> const& a,
            std::pair<std::string, Asset> const& b){return a.first.compare(b.first) < 0;});
}

我在尝试编译时遇到以下错误

Severity    Code    Description Project File    Line
Error   C2280   'AssetManager::Asset &AssetManager::Asset::operator =(const AssetManager::Asset &)': attempting to reference a deleted function c:program files (x86)microsoft visual studio 14.0vcincludeutility  175

我知道问题出在参数上,但我不明白为什么。如果const std::pair<std::string, Asset> const& a是一对字符串和资产的引用,为什么要调用赋值运算符?

排序调用赋值运算符来交换两个无序的元素。在您的案例中使用它是因为没有定义swap(Asset &, Asset &)函数,所以使用默认的swap,它将通过临时复制。

在技术术语中,sort要求它的参数是ValueSwappable,而具有引用成员的类则不是

您需要提供一个交换或分配运算符,以便排序工作。