gpt4 book ai didi

c++ - 删除函数,std::pair 引用

转载 作者:行者123 更新时间:2023-11-30 05:35:27 24 4
gpt4 key购买 nike

我有以下类(class):

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.0\vc\include\utility 175

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

最佳答案

赋值运算符被sort调用来交换两个乱序的元素。它用于您的情况,因为没有定义 swap(Asset &, Asset &) 函数,因此使用默认的 swap 将通过临时复制。

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

您需要提供交换或赋值运算符才能进行排序。

关于c++ - 删除函数,std::pair<std::string, Asset> 引用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33906514/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com