删除的函数,标准::p空气<标准::字符串,资产>引用
Deleted function, std::pair<std::string, Asset> reference
我有以下类:
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
,而具有引用成员的类则不是
您需要提供一个交换或分配运算符,以便排序工作。
相关文章:
- 标准库类型的赋值运算符的引用限定符
- 没有取消引用/解包对象的标准方法?
- 常量转发引用给出错误 C2440:"正在初始化":无法从"常量标准::字符串"转换为"常量标准::字符串 &&"
- 为什么标准不允许通过引用捕获不完整的异常类型?
- 标准库容器结构是否存储副本或引用?
- 是否可以将引用存储在标准::任何中
- 在标准算法中,通过引用捕获更正确
- 如何正确访问通过引用从标准输入传递给构造函数的参数的值
- 使用 C++17 标准对"WinMain@16"的未定义引用
- 使用标准::绑定在可变参数模板中完美转发引用
- 为什么我不能在标准::未来参数中使用引用
- 通过引用查找调用范围的标准引用是什么
- 类型说明符中允许的类型定义的C++11标准引用使用
- 将r值绑定到l值引用是非标准的Microsoft C++扩展
- C++11:常量和引用类型上“auto”操作的标准引用
- 取消引用等于 nullptr 的指针是否是标准的未定义行为?
- 带有标准容器和常量引用的c++代码显示出意外的内存
- SWIG 不可变标准::映射引用
- 通过值或引用传递标准::字符串
- c++ 11标准引用,不使用左值初始化右值引用