In my C++ project I am getting the following error from the compiler: is implicitly…
- User
In my C++ project I am getting the following error from the compiler: is implicitly deleted because the default definition would be ill-formed: 148 | unordered_map() = default; Do you know what can be?
- Assistant
This error usually indicates that the default definition of the
unordered_mapconstructor is trying to perform one or more operations that are not allowed, which causes the constructor to be implicitly deleted.Here are some common reasons that could cause this issue:
-
Member Variables: If the class
unordered_mapcontains member variables that do not have a default constructor, this will cause the default constructor ofunordered_mapto be ill-formed. -
Deleted or Inaccessible Constructor: If one of the member variables has a constructor that is deleted or inaccessible (e.g., private without a public accessor), the default constructor of
unordered_mapwill be ill-formed. -
References and
constMembers: If the classunordered_mapcontains member variables that are references orconst, these members must be initialized in the initializer list of the constructor since they do not have default constructors. -
Array Members: Arrays that do not have default-constructible element types can also cause this problem.
Here’s an example to demonstrate some of these issues:
#include <unordered_map> class NonDefaultConstructible { public: NonDefaultConstructible(int x) {} // No default constructor }; class Example { private: NonDefaultConstructible m; // This member does not have a default constructor // other members... public: std::unordered_map<int, int> unordered_map; // Your declaration Example() = default; // Trying to default this constructor will fail }; int main() { Example ex; // Error here return 0; }In this example, trying to default the constructor of
Examplewill fail becauseNonDefaultConstructibledoes not have a default constructor.Solution
To resolve the issue, you need to provide proper initialization for such members either through a user-defined constructor or initialize them in the member initializer list. Here’s how you can fix the example:
#include <unordered_map> class NonDefaultConstructible { public: NonDefaultConstructible(int x) {} }; class Example { private: NonDefaultConstructible m; // This does not have a default constructor // other members... public: std::unordered_map<int, int> unordered_map; // Define a constructor to initialize members that lack default constructors Example() : m(42) { // Provide necessary initialization } }; int main() { Example ex; // This will work now return 0; }In this fixed example, I provided an explicit constructor for the
Exampleclass to ensure thatNonDefaultConstructibleis properly initialized.Inspect your class for similar issues when you see such errors and make sure all member variables can be properly default-constructed or are otherwise initialized in the constructor.
-
- User
Yes my class does not have a default constructor but at constructor time I need just an empty unordered_map. Suggestion for it?
- Assistant
If your class
unordered_mapdoes not require the initialization of certain members (which do not have default constructors) when it is default-constructed, you should explicitly define a constructor for your class. In this constructor, you can initialize those members properly while leaving theunordered_mapempty.Here's a step-by-step solution based on your requirement to have an empty
unordered_mapat construction time:-
Define a Constructor: Provide an explicit constructor for your class where you initialize all necessary members.
-
Initialize Members Properly: Ensure that all members that do not have default constructors are initialized correctly.
Here's an example of a possible implementation:
#include <unordered_map> class NonDefaultConstructible { public: NonDefaultConstructible(int x) {} }; class MyClass { private: NonDefaultConstructible m; // This member does not have a default constructor std::unordered_map<int, int> my_map; // Your unordered_map public: // Explicit constructor to initialize all members MyClass(int m_initial_value) : m(m_initial_value), my_map() { // The unordered_map `my_map` is default-initialized to an empty map. } // Accessor for the unordered_map std::unordered_map<int, int>& getMap() { return my_map; } }; int main() { MyClass my_instance(42); // Initialize with some value for NonDefaultConstructible std::unordered_map<int, int>& map_ref = my_instance.getMap(); // my_map is empty at this point, but you can manipulate it as you wish map_ref[1] = 100; // Example of inserting an element return 0; }Explanation:
- NonDefaultConstructible: This class has no default constructor and requires an integer to be instantiated.
- MyClass: This class contains a member of type
NonDefaultConstructibleand an unordered map. - Constructor of MyClass: The constructor
MyClass(int m_initial_value)initializes theNonDefaultConstructiblemember. Theunordered_mapmy_mapis default-constructed, which means it starts as an empty map. - Access to unordered_map: We provided a method (
getMap) to access the underlyingunordered_map.
By using this explicit constructor, you ensure that all members are initialized correctly, and your
unordered_mapstarts as an empty map. -
- User
Here a snippet of my code: ~ class Zoo { public: Zoo() : m_zoo() {}; private: std::unordered_map<Animal, std::pair<size_t, size_t>> m_zoo; }; ~ Animal is a class with a specialized constructor and a default one: Animal(std::string _species, Eater _eater, int32_t _average) : m_species(_species), m_eater(_eater); Animal() = default; What is wrong in this snippet?
- Assistant
From the provided code snippet, it appears that the
Zooclass is intended to contain anunordered_mapwithAnimalas the key. TheAnimalclass has both a specialized constructor and a default constructor.However, one important prerequisite for an object to be used as a key in
std::unordered_mapis that the key type must satisfy the requirements ofHashandEqualityComparable. This means theAnimalclass needs to provide hash and equality operations.Here's a list of potential issues and checks that you should verify:
-
Hash Function: The
Animalclass must have astd::hashspecialization to be used as a key instd::unordered_map. -
Equality Operator: The
Animalclass must have an equality comparison operator (operator==).
Here’s how you can define these operations for the
Animalclass:- Define the Hash Function:
#include <string> #include <unordered_map> #include <utility> enum class Eater { Herbivore, Carnivore, Omnivore }; class Animal { public: // Specialized constructor Animal(std::string species, Eater eater, int32_t average) : m_species(std::move(species)), m_eater(eater), m_average(average) {} // Default constructor Animal() = default; // Accessor methods (optional) const std::string& getSpecies() const { return m_species; } Eater getEater() const { return m_eater; } int32_t getAverage() const { return m_average; } // Comparison operator bool operator==(const Animal& other) const { return m_species == other.m_species && m_eater == other.m_eater && m_average == other.m_average; } private: std::string m_species; Eater m_eater; int32_t m_average; }; // Custom hash function for the Animal class namespace std { template <> struct hash<Animal> { std::size_t operator()(const Animal& a) const { using std::hash; using std::size_t; using std::string; // Compute individual hash values for each member and combine them using bitwise operations return ((hash<string>()(a.getSpecies()) ^ (hash<int>()(static_cast<int>(a.getEater())) << 1)) >> 1) ^ (hash<int32_t>()(a.getAverage()) << 1); } }; } class Zoo { public: Zoo() : m_zoo() {}; // Default constructor initializes empty unordered_map private: std::unordered_map<Animal, std::pair<size_t, size_t>> m_zoo; };Explanation:
-
Default and Specialized Constructor:
Animal(std::string species, Eater eater, int32_t average): Specialized constructor.Animal() = default: Default constructor is explicitly declared.
-
Comparison Operator:
operator==: Checks for equality between twoAnimalobjects based on their members.
-
Custom Hash Function:
- Specialize
std::hash<Animal>to provide a hashing mechanism forAnimalobjects. This is required forunordered_mapto compute hash values for its keys.
- Specialize
-
Zoo Class:
- The
m_zoomember is anunordered_mapthat usesAnimalas the key. An explicit default constructor is provided which initializes the unordered_map as empty.
- The
By implementing the hash specialization and equality operator, you enable the
Animalclass to be used as a key instd::unordered_map. This should resolve any compilation issues related to the use ofAnimalin theunordered_mapof theZooclass. -