STL and User-Defined Classes

STL and User-Defined Classes

I’m new to the Standard Template Library. How do I incorporate a user-defined class into a hash_map? What functions do I need to overload? I get compiler errors when I try to insert into my hash_map for even the simplest of classes.

Hash_map is not part of the Standard Template Library, at least officially. Hashed containers were presented to the C++ standardization committee too late, so the committee did not have sufficient time to include them in the C++ Standard.

Nevertheless, several vendors now offer proprietary hashed containers as an extension to Standard. However, these extensions are not compatible, nor is there any guarantee that they will be standardized in the future.

You didn’t post the code that causes compilation errors or the error messages that you received, so I cannot give a specific answer. However, you should check the interaction of namespaces and the hash_map (i.e., is hash_map defined in the namespace STD or perhaps another namespace?).

In addition, make sure that your classes define appropriate default ctor, copy ctor, assignment operator, and destructor. Failing to do so is the most fertile source of compilation errors when using STL containers.


Share the Post: