unordered_map not working in class

Dec 31, 2015 at 4:23am
1
2
3
4
5
6
7
8
9
class State
{
    public:
        State();
        
    protected:
    private:
        std::unordered_map<std::string,std::unique_ptr<int>> objects;
};


This code will not compile. CodeBlocks gives me
c:\mingw\lib\gcc\mingw32\4.8.1\include\c++\bits\hashtable_policy.h||In instantiation of 'std::__detail::_Hash_node<_Value, true>::_Hash_node(_Args&& ...) [with _Args = {const std::pair<const std::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::unique_ptr<int, std::default_delete<int> > >&}; _Value = std::pair<const std::basic_string<char>, std::unique_ptr<int> >]':|
c:\mingw\lib\gcc\mingw32\4.8.1\include\c++\ext\new_allocator.h|120|required from 'void __gnu_cxx::new_allocator<_Tp>::construct(_Up*, _Args&& ...) [with _Up = std::__detail::_Hash_node<std::pair<const std::basic_string<char>, std::unique_ptr<int> >, true>; _Args = {const std::pair<const std::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::unique_ptr<int, std::default_delete<int> > >&}; _Tp = std::__detail::_Hash_node<std::pair<const std::basic_string<char>, std::unique_ptr<int> >, true>]'|
c:\mingw\lib\gcc\mingw32\4.8.1\include\c++\bits\hashtable.h|727|required from 'std::_Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal, _H1, _H2, _Hash, _RehashPolicy, _Traits>::__node_type* std::_Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal, _H1, _H2, _Hash, _RehashPolicy, _Traits>::_M_allocate_node(_Args&& ...) [with _Args = {const std::pair<const std::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::unique_ptr<int, std::default_delete<int> > >&}; _Key = std::basic_string<char>; _Value = std::pair<const std::basic_string<char>, std::un|
c:\mingw\lib\gcc\mingw32\4.8.1\include\c++\bits\hashtable.h|898|required from 'std::_Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal, _H1, _H2, _Hash, _RehashPolicy, _Traits>::_Hashtable(const std::_Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal, _H1, _H2, _Hash, _RehashPolicy, _Traits>&) [with _Key = std::basic_string<char>; _Value = std::pair<const std::basic_string<char>, std::unique_ptr<int> >; _Alloc = std::allocator<std::pair<const std::basic_string<char>, std::unique_ptr<int> > >; _ExtractKey = std::__detail::_Select1st; _Equal = std::equal_to<std::basi|
c:\mingw\lib\gcc\mingw32\4.8.1\include\c++\bits\hashtable.h|425|required from 'std::_Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal, _H1, _H2, _Hash, _RehashPolicy, _Traits>& std::_Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal, _H1, _H2, _Hash, _RehashPolicy, _Traits>::operator=(const std::_Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal, _H1, _H2, _Hash, _RehashPolicy, _Traits>&) [with _Key = std::basic_string<char>; _Value = std::pair<const std::basic_string<char>, std::unique_ptr<int> >; _Alloc = std::allocator<std::pair<const std::basic_string<char>,|
c:\mingw\lib\gcc\mingw32\4.8.1\include\c++\bits\unordered_map.h|97|required from here|
c:\mingw\lib\gcc\mingw32\4.8.1\include\c++\bits\hashtable_policy.h|177|error: use of deleted function 'constexpr std::pair<_T1, _T2>::pair(const std::pair<_T1, _T2>&) [with _T1 = const std::basic_string<char>; _T2 = std::unique_ptr<int>]'|
c:\mingw\lib\gcc\mingw32\4.8.1\include\c++\bits\stl_pair.h|127|note: 'constexpr std::pair<_T1, _T2>::pair(const std::pair<_T1, _T2>&) [with _T1 = const std::basic_string<char>; _T2 = std::unique_ptr<int>]' is implicitly deleted because the default definition would be ill-formed:|
c:\mingw\lib\gcc\mingw32\4.8.1\include\c++\bits\stl_pair.h|127|error: use of deleted function 'std::unique_ptr<_Tp, _Dp>::unique_ptr(const std::unique_ptr<_Tp, _Dp>&) [with _Tp = int; _Dp = std::default_delete<int>]'|
c:\mingw\lib\gcc\mingw32\4.8.1\include\c++\bits\unique_ptr.h|273|error: declared here|
||=== Build failed: 3 error(s), 9 warning(s) (0 minute(s), 1 second(s)) ===|

Strangely, the code compiles when the unordered_map declaration is moved to the main function. Why is this happening?
Last edited on Dec 31, 2015 at 4:57am
Dec 31, 2015 at 9:05am
Strange, the code compiles fine when I add the includes.

1
2
3
#include <unordered_map>
#include <memory>
#include <string> 


Maybe your version of GCC is too old. It compiles fine in GCC 4.9.2. But that doesn't really explain why it works in main but not here. Are you sure this is the first error message in the list?
Dec 31, 2015 at 3:44pm
I just updated GCC to the newest version and I still get these errors. Too many c++ headaches.
Dec 31, 2015 at 3:53pm
Not surprisingly, this compiles
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
class St
{
    public:
        St(){}

    protected:
    private:
        std::unordered_map<std::string,std::unique_ptr<int>> objects;
};


class State
{
    public:
        State(){}

    protected:
    private:
        //std::unordered_map<std::string,std::unique_ptr<int>> objects;
};


This is by far the strangest thing I have experienced.
Last edited on Dec 31, 2015 at 3:54pm
Dec 31, 2015 at 4:06pm
Are you by any chance trying to copy a State object anywhere in your program? It will not work because std::unique_ptr is not copyable. To make it work you would have to implement your own copy constructor/copy assignment operator for the State class, or find a way to avoid the copying from taking place.
Last edited on Dec 31, 2015 at 4:09pm
Dec 31, 2015 at 4:28pm
You're right. I just found a line where a State is being copied. Time to fix.
Dec 31, 2015 at 4:35pm
You're right. I just found a line where a State is being copied. Time to fix.

If your unique_ptr is pointing to a single int, store an int directly instead. If your unique_ptr is pointing to an array, use a container instead. I'm at a loss as to why one would decide to use unique_ptr here.
Dec 31, 2015 at 9:13pm
Actually, the int type is just a placeholder. It was originally of type Object, but I changed it to make the example simpler.
Topic archived. No new replies allowed.