gpt4 book ai didi

c++ - std::unordered_map - Inserting elements is “attempting to reference a deleted function”

转载 作者:行者123 更新时间:2023-12-03 07:52:12 30 4
gpt4 key购买 nike

我有一个std::map,它将“名称”映射到ParticleParticle对象具有一个图标(仅是一个字符)和一个位置(其实质上是带有类包装器的X,Y坐标对)。

class Particle {
public:
Particle(char icon = '\0', Point position);
/* Copy and move constructors and assignment operators */

/* Other stuff */

private:
char icon_;
Point position_;
};
这是 Point类(出于完整性考虑):
class Point {
public:
// Type for specifying a coordinate
using coord_ty = uint16_t;

explicit Point(coord_ty x = 0, coord_ty y = 0) noexcept;

/* Copy and move constructors and operators, as well as
Setters and getters */

[[nodiscard]] bool operator==(const Point&) const noexcept;
[[nodiscard]] bool operator!=(const Point&) const noexcept;

private:
coord_ty x_, y_;
};
在负责将唯一的“名称”( std::string)映射到粒子的类中,我有以下内容:
class Frame {
public:
/* Constructors and other stuff */

private:
std::map<std::string, Particle> particles_;

/**
@brief Removes all duplicate particles

Uses a hashmap of positions to std::pair<string, Particle>* objects to
eliminate all duplicates by the nature of a hashmap. Duplicates are
determined if two different Particles have the same position
*/
void remove_duplicates();
};
这个 remove_duplicates()方法给我带来了麻烦(本文结尾的完整错误日志)。这是函数定义:
void Frame::remove_duplicates() {
// Maps all positions to their name-particle pair in the particles_ map
std::unordered_map<Point, decltype(particles_)::value_type*> positions_to_particles;

for (auto& particle_pair : particles_) {
const auto position = particle_pair.second.position();
positions_to_particles.insert({ position, &particle_pair });
}

// If the 'particles_' map and the 'positions_to_particles' map are the
// same size, then there were no duplicates. Otherwise,
// 'positions_to_particles' would have to be smaller than 'particles_',
// indicating there were duplicates
if (positions_to_particles.size() < particles_.size()) {
// Replace particles_ with a new map with no duplicates
decltype(particles_) new_map;
for (auto& hashpair : positions_to_particles)
new_map.insert({ hashpair.second->first, hashpair.second->second });

particles_ = std::move(new_map);
}
}
在Visual Studio 2019中使用C++ 17,编译时出现以下错误。我知道是上述方法的罪魁祸首,因为它是我所有代码中唯一使用哈希映射的地方。
1>C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.27.29110\include\unordered_map(51,99): error C2280: 'std::_Uhash_compare<_Kty,_Hasher,_Keyeq>::_Uhash_compare(const std::_Uhash_compare<_Kty,_Hasher,_Keyeq> &)': attempting to reference a deleted function
这是完整的构建日志:
1>------ Build started: Project: FrameDesigner, Configuration: Debug x64 ------
1>Using triplet "x64-windows" from "C:\Dev\vcpkg\installed\x64-windows\"
1>Frame.cpp
1>C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.27.29110\include\unordered_map(51,99): error C2280: 'std::_Uhash_compare<_Kty,_Hasher,_Keyeq>::_Uhash_compare(const std::_Uhash_compare<_Kty,_Hasher,_Keyeq> &)': attempting to reference a deleted function
1> with
1> [
1> _Kty=Point,
1> _Hasher=std::hash<Point>,
1> _Keyeq=std::equal_to<Point>
1> ]
1>C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.27.29110\include\xhash(185): message : compiler has generated 'std::_Uhash_compare<_Kty,_Hasher,_Keyeq>::_Uhash_compare' here
1> with
1> [
1> _Kty=Point,
1> _Hasher=std::hash<Point>,
1> _Keyeq=std::equal_to<Point>
1> ]
1>C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.27.29110\include\xhash(185,1): message : 'std::_Uhash_compare<_Kty,_Hasher,_Keyeq>::_Uhash_compare(const std::_Uhash_compare<_Kty,_Hasher,_Keyeq> &)': function was implicitly deleted because a data member invokes a deleted or inaccessible function 'std::_Compressed_pair<_Hasher,std::_Compressed_pair<_Keyeq,float,true>,true>::_Compressed_pair(const std::_Compressed_pair<_Hasher,std::_Compressed_pair<_Keyeq,float,true>,true> &)'
1> with
1> [
1> _Kty=Point,
1> _Hasher=std::hash<Point>,
1> _Keyeq=std::equal_to<Point>
1> ]
1> and
1> [
1> _Hasher=std::hash<Point>,
1> _Keyeq=std::equal_to<Point>
1> ]
1>C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.27.29110\include\xmemory(1350,1): message : 'std::_Compressed_pair<_Hasher,std::_Compressed_pair<_Keyeq,float,true>,true>::_Compressed_pair(const std::_Compressed_pair<_Hasher,std::_Compressed_pair<_Keyeq,float,true>,true> &)': function was implicitly deleted because a base class invokes a deleted or inaccessible function 'std::hash<Point>::hash(const std::hash<Point> &)'
1> with
1> [
1> _Hasher=std::hash<Point>,
1> _Keyeq=std::equal_to<Point>
1> ]
1>C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.27.29110\include\type_traits(2255,1): message : 'std::hash<Point>::hash(const std::hash<Point> &)': function was implicitly deleted because a base class invokes a deleted or inaccessible function 'std::_Conditionally_enabled_hash<_Kty,false>::_Conditionally_enabled_hash(const std::_Conditionally_enabled_hash<_Kty,false> &)'
1> with
1> [
1> _Kty=Point
1> ]
1>C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.27.29110\include\type_traits(2240,5): message : 'std::_Conditionally_enabled_hash<_Kty,false>::_Conditionally_enabled_hash(const std::_Conditionally_enabled_hash<_Kty,false> &)': function was explicitly deleted
1> with
1> [
1> _Kty=Point
1> ]
1>C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.27.29110\include\unordered_map(51): message : while compiling class template member function 'std::_Umap_traits<_Kty,_Ty,std::_Uhash_compare<_Kty,_Hasher,_Keyeq>,_Alloc,false>::_Umap_traits(const _Tr &) noexcept(false)'
1> with
1> [
1> _Kty=Point,
1> _Ty=std::pair<const std::string,Particle> *,
1> _Hasher=std::hash<Point>,
1> _Keyeq=std::equal_to<Point>,
1> _Alloc=std::allocator<std::pair<const Point,std::pair<const std::string,Particle> *>>,
1> _Tr=std::_Uhash_compare<Point,std::hash<Point>,std::equal_to<Point>>
1> ]
1>C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.27.29110\include\xhash(391): message : see reference to function template instantiation 'std::_Umap_traits<_Kty,_Ty,std::_Uhash_compare<_Kty,_Hasher,_Keyeq>,_Alloc,false>::_Umap_traits(const _Tr &) noexcept(false)' being compiled
1> with
1> [
1> _Kty=Point,
1> _Ty=std::pair<const std::string,Particle> *,
1> _Hasher=std::hash<Point>,
1> _Keyeq=std::equal_to<Point>,
1> _Alloc=std::allocator<std::pair<const Point,std::pair<const std::string,Particle> *>>,
1> _Tr=std::_Uhash_compare<Point,std::hash<Point>,std::equal_to<Point>>
1> ]
1>C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.27.29110\include\xhash(342): message : see reference to class template instantiation 'std::_Umap_traits<_Kty,_Ty,std::_Uhash_compare<_Kty,_Hasher,_Keyeq>,_Alloc,false>' being compiled
1> with
1> [
1> _Kty=Point,
1> _Ty=std::pair<const std::string,Particle> *,
1> _Hasher=std::hash<Point>,
1> _Keyeq=std::equal_to<Point>,
1> _Alloc=std::allocator<std::pair<const Point,std::pair<const std::string,Particle> *>>
1> ]
1>C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.27.29110\include\unordered_map(69): message : see reference to class template instantiation 'std::_Hash<std::_Umap_traits<_Kty,_Ty,std::_Uhash_compare<_Kty,_Hasher,_Keyeq>,_Alloc,false>>' being compiled
1> with
1> [
1> _Kty=Point,
1> _Ty=std::pair<const std::string,Particle> *,
1> _Hasher=std::hash<Point>,
1> _Keyeq=std::equal_to<Point>,
1> _Alloc=std::allocator<std::pair<const Point,std::pair<const std::string,Particle> *>>
1> ]
1>C:\[PATH TO FILE]\Frame.cpp(134): message : see reference to class template instantiation 'std::unordered_map<Point,std::pair<const std::string,Particle> *,std::hash<Point>,std::equal_to<Point>,std::allocator<std::pair<const Point,std::pair<const std::string,Particle> *>>>' being compiled
1>Done building project "FrameDesigner.vcxproj" -- FAILED.
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

最佳答案

template <class Key,
class Ty,
class Hash = std::hash<Key>,
class Pred = std::equal_to<Key>,
class Alloc = std::allocator<std::pair<const Key, Ty>>>
class unordered_map;
根据定义,您必须为键提供Hash和Equal函数,并且键必须具有默认构造函数!当您声明std::unordered_map 时,编译器将查找“Point”的默认构造函数

关于c++ - std::unordered_map - Inserting elements is “attempting to reference a deleted function” ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65755063/

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