Errors building STLPort - c++
I'm trying to use STLPort in my project, and when I follow the directions in INSTALL.unixes I get compile errors.
I ran ./configure, then make, the output is below. I'm using make 3.81 and gcc 4.6.1 on Ubuntu.
Edit: I tried adding --with-extra-cxxflags="-std=c++ox" and I get a different and smaller set of errors, listed below.
In file included from ../stlport/memory:45:0,
from reachable.cc:16:
../stlport/stl/_shared_ptr.h:377:43: error: function definition does not declare parameters
../stlport/stl/_shared_ptr.h:421:39: error: declaration of ‘operator=’ as non-function
../stlport/stl/_shared_ptr.h:421:26: error: expected ‘;’ at end of member declaration
../stlport/stl/_shared_ptr.h:421:39: error: function definition does not declare parameters
../stlport/stl/_shared_ptr.h:463:17: error: ‘nullptr_t’ has not been declared
../stlport/stl/_shared_ptr.h:536:43: error: function definition does not declare parameters
../stlport/stl/_shared_ptr.h:552:39: error: declaration of ‘operator=’ as non-function
../stlport/stl/_shared_ptr.h:552:26: error: expected ‘;’ at end of member declaration
../stlport/stl/_shared_ptr.h:552:39: error: function definition does not declare parameters
../stlport/stl/_shared_ptr.h:591:17: error: ‘nullptr_t’ has not been declared
In file included from ../stlport/memory:45:0,
from reachable.cc:16:
../stlport/stl/_shared_ptr.h:643:45: error: ‘nullptr_t’ has not been declared
../stlport/stl/_shared_ptr.h:647:18: error: declaration of ‘operator==’ as non-function
../stlport/stl/_shared_ptr.h:647:18: error: ‘nullptr_t’ was not declared in this scope
../stlport/stl/_shared_ptr.h:647:18: note: suggested alternative:
/usr/include/c++/4.6/i686-linux-gnu/./bits/c++config.h:159:29: note: ‘std::nullptr_t’
../stlport/stl/_shared_ptr.h:647:29: error: expected primary-expression before ‘const’
../stlport/stl/_shared_ptr.h:647:50: error: expected primary-expression before ‘>’ token
../stlport/stl/_shared_ptr.h:647:53: error: ‘x’ was not declared in this scope
../stlport/stl/_shared_ptr.h:651:45: error: ‘nullptr_t’ has not been declared
../stlport/stl/_shared_ptr.h:655:18: error: declaration of ‘operator!=’ as non-function
../stlport/stl/_shared_ptr.h:655:18: error: ‘nullptr_t’ was not declared in this scope
../stlport/stl/_shared_ptr.h:655:18: note: suggested alternative:
/usr/include/c++/4.6/i686-linux-gnu/./bits/c++config.h:159:29: note: ‘std::nullptr_t’
../stlport/stl/_shared_ptr.h:655:29: error: expected primary-expression before ‘const’
../stlport/stl/_shared_ptr.h:655:50: error: expected primary-expression before ‘>’ token
../stlport/stl/_shared_ptr.h:655:53: error: ‘x’ was not declared in this scope
../stlport/stl/_shared_ptr.h:660:44: error: ‘nullptr_t’ has not been declared
../stlport/stl/_shared_ptr.h:665:17: error: declaration of ‘operator<’ as non-function
../stlport/stl/_shared_ptr.h:665:17: error: ‘nullptr_t’ was not declared in this scope
../stlport/stl/_shared_ptr.h:665:17: note: suggested alternative:
/usr/include/c++/4.6/i686-linux-gnu/./bits/c++config.h:159:29: note: ‘std::nullptr_t’
../stlport/stl/_shared_ptr.h:665:28: error: expected primary-expression before ‘const’
../stlport/stl/_shared_ptr.h:665:49: error: expected primary-expression before ‘>’ token
../stlport/stl/_shared_ptr.h:665:52: error: ‘x’ was not declared in this scope
../stlport/stl/_shared_ptr.h:669:44: error: ‘nullptr_t’ has not been declared
../stlport/stl/_shared_ptr.h:673:17: error: declaration of ‘operator>’ as non-function
../stlport/stl/_shared_ptr.h:673:17: error: ‘nullptr_t’ was not declared in this scope
../stlport/stl/_shared_ptr.h:673:17: note: suggested alternative:
/usr/include/c++/4.6/i686-linux-gnu/./bits/c++config.h:159:29: note: ‘std::nullptr_t’
../stlport/stl/_shared_ptr.h:673:28: error: expected primary-expression before ‘const’
../stlport/stl/_shared_ptr.h:673:49: error: expected primary-expression before ‘>’ token
../stlport/stl/_shared_ptr.h:673:52: error: ‘x’ was not declared in this scope
../stlport/stl/_shared_ptr.h:677:45: error: ‘nullptr_t’ has not been declared
../stlport/stl/_shared_ptr.h:681:18: error: declaration of ‘operator<=’ as non-function
../stlport/stl/_shared_ptr.h:681:18: error: ‘nullptr_t’ was not declared in this scope
../stlport/stl/_shared_ptr.h:681:18: note: suggested alternative:
/usr/include/c++/4.6/i686-linux-gnu/./bits/c++config.h:159:29: note: ‘std::nullptr_t’
../stlport/stl/_shared_ptr.h:681:29: error: expected primary-expression before ‘const’
../stlport/stl/_shared_ptr.h:681:50: error: expected primary-expression before ‘>’ token
../stlport/stl/_shared_ptr.h:681:53: error: ‘x’ was not declared in this scope
../stlport/stl/_shared_ptr.h:685:45: error: ‘nullptr_t’ has not been declared
../stlport/stl/_shared_ptr.h:689:18: error: declaration of ‘operator>=’ as non-function
../stlport/stl/_shared_ptr.h:689:18: error: ‘nullptr_t’ was not declared in this scope
../stlport/stl/_shared_ptr.h:689:18: note: suggested alternative:
/usr/include/c++/4.6/i686-linux-gnu/./bits/c++config.h:159:29: note: ‘std::nullptr_t’
../stlport/stl/_shared_ptr.h:689:29: error: expected primary-expression before ‘const’
../stlport/stl/_shared_ptr.h:689:50: error: expected primary-expression before ‘>’ token
../stlport/stl/_shared_ptr.h:689:53: error: ‘x’ was not declared in this scope
../stlport/stl/_shared_ptr.h:775:26: error: expected ‘)’ before ‘,’ token
../stlport/stl/_shared_ptr.h:790:26: error: expected ‘)’ before ‘,’ token
../stlport/stl/_shared_ptr.h:917:43: error: function definition does not declare parameters
../stlport/stl/_shared_ptr.h:1160:26: error: expected ‘)’ before ‘,’ token
../stlport/stl/_shared_ptr.h:1175:26: error: expected ‘)’ before ‘,’ token
../stlport/stl/_shared_ptr.h:1296:43: error: function definition does not declare parameters
../stlport/stl/_shared_ptr.h:1553:42: error: ‘nullptr_t’ has not been declared
../stlport/stl/_shared_ptr.h:1557:18: error: declaration of ‘operator==’ as non-function
../stlport/stl/_shared_ptr.h:1557:18: error: ‘nullptr_t’ was not declared in this scope
../stlport/stl/_shared_ptr.h:1557:18: note: suggested alternative:
/usr/include/c++/4.6/i686-linux-gnu/./bits/c++config.h:159:29: note: ‘std::nullptr_t’
../stlport/stl/_shared_ptr.h:1557:29: error: expected primary-expression before ‘const’
../stlport/stl/_shared_ptr.h:1561:42: error: ‘nullptr_t’ has not been declared
../stlport/stl/_shared_ptr.h:1565:18: error: declaration of ‘operator!=’ as non-function
../stlport/stl/_shared_ptr.h:1565:18: error: ‘nullptr_t’ was not declared in this scope
../stlport/stl/_shared_ptr.h:1565:18: note: suggested alternative:
/usr/include/c++/4.6/i686-linux-gnu/./bits/c++config.h:159:29: note: ‘std::nullptr_t’
../stlport/stl/_shared_ptr.h:1565:29: error: expected primary-expression before ‘const’
../stlport/stl/_shared_ptr.h:1569:41: error: ‘nullptr_t’ has not been declared
../stlport/stl/_shared_ptr.h:1573:17: error: declaration of ‘operator<’ as non-function
../stlport/stl/_shared_ptr.h:1573:17: error: ‘nullptr_t’ was not declared in this scope
../stlport/stl/_shared_ptr.h:1573:17: note: suggested alternative:
/usr/include/c++/4.6/i686-linux-gnu/./bits/c++config.h:159:29: note: ‘std::nullptr_t’
../stlport/stl/_shared_ptr.h:1573:28: error: expected primary-expression before ‘const’
../stlport/stl/_shared_ptr.h:1577:42: error: ‘nullptr_t’ has not been declared
../stlport/stl/_shared_ptr.h:1581:18: error: declaration of ‘operator<=’ as non-function
../stlport/stl/_shared_ptr.h:1581:18: error: ‘nullptr_t’ was not declared in this scope
../stlport/stl/_shared_ptr.h:1581:18: note: suggested alternative:
/usr/include/c++/4.6/i686-linux-gnu/./bits/c++config.h:159:29: note: ‘std::nullptr_t’
../stlport/stl/_shared_ptr.h:1581:29: error: expected primary-expression before ‘const’
../stlport/stl/_shared_ptr.h:1585:41: error: ‘nullptr_t’ has not been declared
../stlport/stl/_shared_ptr.h:1589:17: error: declaration of ‘operator>’ as non-function
../stlport/stl/_shared_ptr.h:1589:17: error: ‘nullptr_t’ was not declared in this scope
../stlport/stl/_shared_ptr.h:1589:17: note: suggested alternative:
/usr/include/c++/4.6/i686-linux-gnu/./bits/c++config.h:159:29: note: ‘std::nullptr_t’
../stlport/stl/_shared_ptr.h:1589:28: error: expected primary-expression before ‘const’
../stlport/stl/_shared_ptr.h:1593:42: error: ‘nullptr_t’ has not been declared
../stlport/stl/_shared_ptr.h:1597:18: error: declaration of ‘operator>=’ as non-function
../stlport/stl/_shared_ptr.h:1597:18: error: ‘nullptr_t’ was not declared in this scope
../stlport/stl/_shared_ptr.h:1597:18: note: suggested alternative:
/usr/include/c++/4.6/i686-linux-gnu/./bits/c++config.h:159:29: note: ‘std::nullptr_t’
../stlport/stl/_shared_ptr.h:1597:29: error: expected primary-expression before ‘const’
In file included from ../stlport/stl/_hashtable.h:541:0,
from ../stlport/stl/_unordered_map.h:24,
from ../stlport/unordered_map:28,
from reachable.cc:17:
../stlport/stl/_hashtable.c: In member function ‘stlp_std::pair<stlp_std::priv::_Ht_iterator<typename stlp_std::forward_list<_Val, _All>::iterator, typename _Traits::_NonConstTraits>, bool> stlp_std::hashtable<_Val, _Key, _HF, _Traits, _ExK, _EqK, _All>::insert_unique_noresize(const value_type&) [with _Val = stlp_std::pair<void* const, long unsigned int>, _Key = void*, _HF = stlp_std::hash<void*>, _Traits = stlp_std::priv::_UnorderedMapTraitsT<stlp_std::pair<void* const, long unsigned int> >, _ExK = stlp_std::priv::_Select1st<stlp_std::pair<void* const, long unsigned int> >, _EqK = stlp_std::equal_to<void*>, _All = stlp_std::allocator<stlp_std::pair<void* const, long unsigned int> >, typename _Traits::_NonConstTraits = stlp_std::priv::_NonLocalUnorderedMapTraitsT<stlp_std::pair<void* const, long unsigned int> >, typename stlp_std::forward_list<_Val, _All>::iterator = stlp_std::priv::_Slist_iterator<stlp_std::pair<void* const, long unsigned int>, stlp_std::_Nonconst_traits<stlp_std::pair<void* const, long unsigned int> > >, stlp_std::hashtable<_Val, _Key, _HF, _Traits, _ExK, _EqK, _All>::value_type = stlp_std::pair<void* const, long unsigned int>]’:
../stlport/stl/_hashtable.c:251:41: instantiated from ‘stlp_std::hashtable<_Val, _Key, _HF, _Traits, _ExK, _EqK, _All>::reference stlp_std::hashtable<_Val, _Key, _HF, _Traits, _ExK, _EqK, _All>::_M_insert(const value_type&) [with _Val = stlp_std::pair<void* const, long unsigned int>, _Key = void*, _HF = stlp_std::hash<void*>, _Traits = stlp_std::priv::_UnorderedMapTraitsT<stlp_std::pair<void* const, long unsigned int> >, _ExK = stlp_std::priv::_Select1st<stlp_std::pair<void* const, long unsigned int> >, _EqK = stlp_std::equal_to<void*>, _All = stlp_std::allocator<stlp_std::pair<void* const, long unsigned int> >, stlp_std::hashtable<_Val, _Key, _HF, _Traits, _ExK, _EqK, _All>::reference = stlp_std::pair<void* const, long unsigned int>&, stlp_std::hashtable<_Val, _Key, _HF, _Traits, _ExK, _EqK, _All>::value_type = stlp_std::pair<void* const, long unsigned int>]’
../stlport/stl/_unordered_map.h:130:22: instantiated from ‘_Tp& stlp_std::unordered_map<_Key, _Tp, _HashFcn, _EqualKey, _Alloc>::operator[](const _KT&) [with _KT = void*, _Key = void*, _Tp = long unsigned int, _HashFcn = stlp_std::hash<void*>, _EqualKey = stlp_std::equal_to<void*>, _Alloc = stlp_std::allocator<stlp_std::pair<void* const, long unsigned int> >]’
reachable.cc:45:27: instantiated from here
../stlport/stl/_hashtable.c:206:59: error: use of deleted function ‘stlp_std::pair<_T1, _T2>::pair(stlp_std::pair<_T1, _T2>&&) [with _T1 = stlp_std::priv::_Ht_iterator<stlp_std::priv::_Slist_iterator<stlp_std::pair<void* const, long unsigned int>, stlp_std::_Nonconst_traits<stlp_std::pair<void* const, long unsigned int> > >, stlp_std::priv::_NonLocalUnorderedMapTraitsT<stlp_std::pair<void* const, long unsigned int> > >, _T2 = bool, stlp_std::pair<_T1, _T2> = stlp_std::pair<stlp_std::priv::_Ht_iterator<stlp_std::priv::_Slist_iterator<stlp_std::pair<void* const, long unsigned int>, stlp_std::_Nonconst_traits<stlp_std::pair<void* const, long unsigned int> > >, stlp_std::priv::_NonLocalUnorderedMapTraitsT<stlp_std::pair<void* const, long unsigned int> > >, bool>]’
In file included from ../stlport/stl/_alloc.h:53:0,
from ../stlport/memory:30,
from reachable.cc:16:
../stlport/utility:140:3: error: ‘stlp_std::pair<_T1, _T2>::pair(stlp_std::pair<_T1, _T2>&&) [with _T1 = stlp_std::priv::_Ht_iterator<stlp_std::priv::_Slist_iterator<stlp_std::pair<void* const, long unsigned int>, stlp_std::_Nonconst_traits<stlp_std::pair<void* const, long unsigned int> > >, stlp_std::priv::_NonLocalUnorderedMapTraitsT<stlp_std::pair<void* const, long unsigned int> > >, _T2 = bool, stlp_std::pair<_T1, _T2> = stlp_std::pair<stlp_std::priv::_Ht_iterator<stlp_std::priv::_Slist_iterator<stlp_std::pair<void* const, long unsigned int>, stlp_std::_Nonconst_traits<stlp_std::pair<void* const, long unsigned int> > >, stlp_std::priv::_NonLocalUnorderedMapTraitsT<stlp_std::pair<void* const, long unsigned int> > >, bool>]’ is implicitly deleted because the default definition would be ill-formed:
../stlport/utility:140:3: error: non-static data member ‘stlp_std::pair<stlp_std::priv::_Ht_iterator<stlp_std::priv::_Slist_iterator<stlp_std::pair<void* const, long unsigned int>, stlp_std::_Nonconst_traits<stlp_std::pair<void* const, long unsigned int> > >, stlp_std::priv::_NonLocalUnorderedMapTraitsT<stlp_std::pair<void* const, long unsigned int> > >, bool>::first’ does not have a move constructor or trivial copy constructor
In file included from ../stlport/stl/_hashtable.h:541:0,
from ../stlport/stl/_unordered_map.h:24,
from ../stlport/unordered_map:28,
from reachable.cc:17:
../stlport/stl/_hashtable.c:251:41: instantiated from ‘stlp_std::hashtable<_Val, _Key, _HF, _Traits, _ExK, _EqK, _All>::reference stlp_std::hashtable<_Val, _Key, _HF, _Traits, _ExK, _EqK, _All>::_M_insert(const value_type&) [with _Val = stlp_std::pair<void* const, long unsigned int>, _Key = void*, _HF = stlp_std::hash<void*>, _Traits = stlp_std::priv::_UnorderedMapTraitsT<stlp_std::pair<void* const, long unsigned int> >, _ExK = stlp_std::priv::_Select1st<stlp_std::pair<void* const, long unsigned int> >, _EqK = stlp_std::equal_to<void*>, _All = stlp_std::allocator<stlp_std::pair<void* const, long unsigned int> >, stlp_std::hashtable<_Val, _Key, _HF, _Traits, _ExK, _EqK, _All>::reference = stlp_std::pair<void* const, long unsigned int>&, stlp_std::hashtable<_Val, _Key, _HF, _Traits, _ExK, _EqK, _All>::value_type = stlp_std::pair<void* const, long unsigned int>]’
../stlport/stl/_unordered_map.h:130:22: instantiated from ‘_Tp& stlp_std::unordered_map<_Key, _Tp, _HashFcn, _EqualKey, _Alloc>::operator[](const _KT&) [with _KT = void*, _Key = void*, _Tp = long unsigned int, _HashFcn = stlp_std::hash<void*>, _EqualKey = stlp_std::equal_to<void*>, _Alloc = stlp_std::allocator<stlp_std::pair<void* const, long unsigned int> >]’
reachable.cc:45:27: instantiated from here
../stlport/stl/_hashtable.c:216:54: error: use of deleted function ‘stlp_std::pair<_T1, _T2>::pair(stlp_std::pair<_T1, _T2>&&) [with _T1 = stlp_std::priv::_Ht_iterator<stlp_std::priv::_Slist_iterator<stlp_std::pair<void* const, long unsigned int>, stlp_std::_Nonconst_traits<stlp_std::pair<void* const, long unsigned int> > >, stlp_std::priv::_NonLocalUnorderedMapTraitsT<stlp_std::pair<void* const, long unsigned int> > >, _T2 = bool, stlp_std::pair<_T1, _T2> = stlp_std::pair<stlp_std::priv::_Ht_iterator<stlp_std::priv::_Slist_iterator<stlp_std::pair<void* const, long unsigned int>, stlp_std::_Nonconst_traits<stlp_std::pair<void* const, long unsigned int> > >, stlp_std::priv::_NonLocalUnorderedMapTraitsT<stlp_std::pair<void* const, long unsigned int> > >, bool>]’
../stlport/stl/_hashtable.c:251:41: instantiated from ‘stlp_std::hashtable<_Val, _Key, _HF, _Traits, _ExK, _EqK, _All>::reference stlp_std::hashtable<_Val, _Key, _HF, _Traits, _ExK, _EqK, _All>::_M_insert(const value_type&) [with _Val = stlp_std::pair<void* const, long unsigned int>, _Key = void*, _HF = stlp_std::hash<void*>, _Traits = stlp_std::priv::_UnorderedMapTraitsT<stlp_std::pair<void* const, long unsigned int> >, _ExK = stlp_std::priv::_Select1st<stlp_std::pair<void* const, long unsigned int> >, _EqK = stlp_std::equal_to<void*>, _All = stlp_std::allocator<stlp_std::pair<void* const, long unsigned int> >, stlp_std::hashtable<_Val, _Key, _HF, _Traits, _ExK, _EqK, _All>::reference = stlp_std::pair<void* const, long unsigned int>&, stlp_std::hashtable<_Val, _Key, _HF, _Traits, _ExK, _EqK, _All>::value_type = stlp_std::pair<void* const, long unsigned int>]’
../stlport/stl/_unordered_map.h:130:22: instantiated from ‘_Tp& stlp_std::unordered_map<_Key, _Tp, _HashFcn, _EqualKey, _Alloc>::operator[](const _KT&) [with _KT = void*, _Key = void*, _Tp = long unsigned int, _HashFcn = stlp_std::hash<void*>, _EqualKey = stlp_std::equal_to<void*>, _Alloc = stlp_std::allocator<stlp_std::pair<void* const, long unsigned int> >]’
reachable.cc:45:27: instantiated from here
../stlport/stl/_hashtable.c:219:67: error: use of deleted function ‘stlp_std::pair<_T1, _T2>::pair(stlp_std::pair<_T1, _T2>&&) [with _T1 = stlp_std::priv::_Ht_iterator<stlp_std::priv::_Slist_iterator<stlp_std::pair<void* const, long unsigned int>, stlp_std::_Nonconst_traits<stlp_std::pair<void* const, long unsigned int> > >, stlp_std::priv::_NonLocalUnorderedMapTraitsT<stlp_std::pair<void* const, long unsigned int> > >, _T2 = bool, stlp_std::pair<_T1, _T2> = stlp_std::pair<stlp_std::priv::_Ht_iterator<stlp_std::priv::_Slist_iterator<stlp_std::pair<void* const, long unsigned int>, stlp_std::_Nonconst_traits<stlp_std::pair<void* const, long unsigned int> > >, stlp_std::priv::_NonLocalUnorderedMapTraitsT<stlp_std::pair<void* const, long unsigned int> > >, bool>]’
make[1]: *** [obj/gcc/so/reachable.o] Error 1
make: *** [release-shared] Error 2
It turns out the source I got my copy of STLPort from, git://stlport.git.sourceforge.net/gitroot/stlport/stlport, is much more bleeding edge than I thought. If I grab the stable build from SourceForge it compiles fine.
Related
passing const iterator as ‘this’ argument discards qualifiers while using vector::erase in a template
I am trying to remove the first value of a vector of template typenames. Using the erase() method in vector stl, I keep getting this error: TTrie.inc:56:19: error: passing ‘const std::vector<char, std::allocator<char> >’ as ‘this’ argument discards qualifiers [-fpermissive] 56 | sequence.erase(it); | ~~~~~~~~~~~~~~^~~~ In file included from /usr/include/c++/10/vector:67, from TTrie.h:5, from TTrieTest.cpp:1: /usr/include/c++/10/bits/stl_vector.h:1430:7: note: in call to ‘std::vector<_Tp, _Alloc>::iterator std::vector<_Tp, _Alloc>::erase(std::vector<_Tp, _Alloc>::const_iterator) [with _Tp = char; _Alloc = std::allocator<char>; std::vector<_Tp, _Alloc>::iterator = std::vector<char, std::allocator<char> >::iterator; std::vector<_Tp, _Alloc>::const_iterator = std::vector<char, std::allocator<char> >::const_iterator]’ 1430 | erase(const_iterator __position) | ^~~~~ In file included from TTrie.h:115, from TTrieTest.cpp:1: TTrie.inc: In instantiation of ‘TTrie<DataType>& TTrie<DataType>::operator+=(const std::vector<DataType>&) [with DataType = std::__cxx11::basic_string<char>]’: TTrieTest.cpp:93:10: required from here TTrie.inc:56:19: error: passing ‘const std::vector<std::__cxx11::basic_string<char> >’ as ‘this’ argument discards qualifiers [-fpermissive] 56 | sequence.erase(it); | ~~~~~~~~~~~~~~^~~~ In file included from /usr/include/c++/10/vector:67, from TTrie.h:5, from TTrieTest.cpp:1: /usr/include/c++/10/bits/stl_vector.h:1430:7: note: in call to ‘std::vector<_Tp, _Alloc>::iterator std::vector<_Tp, _Alloc>::erase(std::vector<_Tp, _Alloc>::const_iterator) [with _Tp = std::__cxx11::basic_string<char>; _Alloc = std::allocator<std::__cxx11::basic_string<char> >; std::vector<_Tp, _Alloc>::iterator = std::vector<std::__cxx11::basic_string<char> >::iterator; std::vector<_Tp, _Alloc>::const_iterator = std::vector<std::__cxx11::basic_string<char> >::const_iterator]’ 1430 | erase(const_iterator __position) | ^~~~~ In file included from TTrie.h:115, from TTrieTest.cpp:1: Is it having trouble because sequence should be const? Am I declaring the iterator wrong somehow? Here is my code. Please let me know If you have any questions: template<typename DataType> TTrie<DataType>& TTrie<DataType>::operator+=(const std::vector<DataType>& sequence) { const DataType c = sequence[0]; const TTrie<DataType>* child = getChild(c); //Returns a pointer to a child node or a nullptr if (!child) { TTrie<DataType>* n = new TTrie<DataType>(c); edgeMap[c] = n; } if(sequence.size() > 1) { typename std::vector<DataType>::const_iterator it; it = sequence.cbegin(); //First value of sequence sequence.erase(it); *edgeMap[c] += sequence; } return *this; }
Your sequence parameter is a reference to a const std::vector<DataType> object. But std::vector::erase() is not qualified as being a const method, as it modifies the content of the vector, so it cannot be called on a const object. That is what the error message is trying to tell you - that erase() is being called on a const object.
Inserting a boost::unique_ptr in std::map
I'm trying to store a boost::unique_ptr in a std::map. The compiler I'm using is g++ 3.4 (/usr/bin/g++34 -I./boost_1_62_0/ test_boost_unique_ptr.cpp). #include <iostream> #include <map> #include <boost/move/unique_ptr.hpp> #include <boost/move/make_unique.hpp> #include <boost/ptr_container/ptr_map.hpp> using namespace boost::movelib; typedef std::map<const int, unique_ptr<int> > MGR_T; typedef MGR_T::const_iterator MGR_ITER_T; MGR_T mgrPluginCache; int main() { unique_ptr<int> p (new int(10)); std::cout << "p = " << *p << std::endl; mgrPluginCache.insert(std::make_pair(1, boost::move(p))); // mgrPluginCache[0] = (boost::move(p)); return 0; } This results in compilation error: In function `int main()': ./boost_1_62_0/boost/move/core.hpp:94: error: `boost::rv<T>::rv(const boost::rv<T>&) [with T = boost::movelib::unique_ptr<int, boost::movelib::default_delete<int> >]' is private test_boost_unique_ptr.cpp:37: error: within this context test_boost_unique_ptr.cpp:37: error: initializing argument 2 of `std::pair<_T1, _T2> std::make_pair(_T1, _T2) [with _T1 = int, _T2 = boost::rv<boost::movelib::unique_ptr<int, boost::movelib::default_delete<int> > >]' ./boost_1_62_0/boost/move/core.hpp:93: error: `boost::rv<T>::~rv() [with T = boost::movelib::unique_ptr<int, boost::movelib::default_delete<int> >]' is private test_boost_unique_ptr.cpp:37: error: within this context test_boost_unique_ptr.cpp: In destructor `std::pair<int, boost::rv<boost::movelib::unique_ptr<int, boost::movelib::default_delete<int> > > >::~pair()': ./boost_1_62_0/boost/move/core.hpp:93: error: `boost::rv<T>::~rv() [with T = boost::movelib::unique_ptr<int, boost::movelib::default_delete<int> >]' is private test_boost_unique_ptr.cpp:37: error: within this context test_boost_unique_ptr.cpp: In copy constructor `std::pair<int, boost::rv<boost::movelib::unique_ptr<int, boost::movelib::default_delete<int> > > >::pair(const std::pair<int, boost::rv<boost::movelib::unique_ptr<int, boost::movelib::default_delete<int> > > >&)': ./boost_1_62_0/boost/move/core.hpp:94: error: `boost::rv<T>::rv(const boost::rv<T>&) [with T = boost::movelib::unique_ptr<int, boost::movelib::default_delete<int> >]' is private test_boost_unique_ptr.cpp:37: error: within this context ./boost_1_62_0/boost/move/core.hpp:93: error: `boost::rv<T>::~rv() [with T = boost::movelib::unique_ptr<int, boost::movelib::default_delete<int> >]' is private test_boost_unique_ptr.cpp:37: error: within this context ./boost_1_62_0/boost/ptr_container/ptr_map_adapter.hpp: In member function `void boost::ptr_map_adapter<T, VoidPtrMap, CloneAllocator, Ordered>::insert(const Range&) [with Range = std::pair<int, boost::rv<boost::movelib::unique_ptr<int, boost::movelib::default_delete<int> > > >, T = boost::movelib::unique_ptr<int, boost::movelib::default_delete<int> >, VoidPtrMap = std::map<const int, void*, std::less<const int>, std::allocator<std::pair<const int, void*> > >, CloneAllocator = boost::heap_clone_allocator, bool Ordered = true]': test_boost_unique_ptr.cpp:37: instantiated from here ./boost_1_62_0/boost/ptr_container/ptr_map_adapter.hpp:518: error: no matching function for call to `begin(const std::pair<int, boost::rv<boost::movelib::unique_ptr<int, boost::movelib::default_delete<int> > > >&)' ./boost_1_62_0/boost/ptr_container/ptr_map_adapter.hpp:518: error: no matching function for call to `end(const std::pair<int, boost::rv<boost::movelib::unique_ptr<int, boost::movelib::default_delete<int> > > >&)' /usr/lib/gcc/x86_64-redhat-linux/3.4.6/../../../../include/c++/3.4.6/bits/stl_pair.h: In constructor `std::pair<_T1, _T2>::pair(const _T1&, const _T2&) [with _T1 = int, _T2 = boost::rv<boost::movelib::unique_ptr<int, boost::movelib::default_delete<int> > >]': /usr/lib/gcc/x86_64-redhat-linux/3.4.6/../../../../include/c++/3.4.6/bits/stl_pair.h:144: instantiated from `std::pair<_T1, _T2> std::make_pair(_T1, _T2) [with _T1 = int, _T2 = boost::rv<boost::movelib::unique_ptr<int, boost::movelib::default_delete<int> > >]' test_boost_unique_ptr.cpp:37: instantiated from here ./boost_1_62_0/boost/move/core.hpp:94: error: `boost::rv<T>::rv(const boost::rv<T>&) [with T = boost::movelib::unique_ptr<int, boost::movelib::default_delete<int> >]' is private /usr/lib/gcc/x86_64-redhat-linux/3.4.6/../../../../include/c++/3.4.6/bits/stl_pair.h:85: error: within this context /usr/lib/gcc/x86_64-redhat-linux/3.4.6/../../../../include/c++/3.4.6/bits/stl_pair.h:144: instantiated from `std::pair<_T1, _T2> std::make_pair(_T1, _T2) [with _T1 = int, _T2 = boost::rv<boost::movelib::unique_ptr<int, boost::movelib::default_delete<int> > >]' test_boost_unique_ptr.cpp:37: instantiated from here ./boost_1_62_0/boost/move/core.hpp:93: error: `boost::rv<T>::~rv() [with T = boost::movelib::unique_ptr<int, boost::movelib::default_delete<int> >]' is private /usr/lib/gcc/x86_64-redhat-linux/3.4.6/../../../../include/c++/3.4.6/bits/stl_pair.h:85: error: within this context Is there a way to store the boost::unique_ptr in std::map? Is it allowed in earlier versions? Can we do this any other way? I cannot change the compiler, the code I'm working on is very old and has not been modified in long time.
value_type of std::map requires copy constructor for insert. boost::unique_ptr have a private copy constructor (which could have been deleted I suppose) You can simply use emplace (you'll also need to std::move or boost::move it): mgrPluginCache.emplace(1, boost::move(foo)); But... if you have std::map::emplace then your compiler supports c++11, then you may consider using std::unique_ptr instead of boost. P.S. g++ 3.4.6 is +10 years old and is likely to break something in recent boost versions. Is there any specific reason to use an outdated compiler?
Taking 2D vector out of 3D Vector and replace with other 2D-Vector
I want to save for an game my 2D-Vector std::vector<std::vector<std::string>> _map in an 3D-Vector std::vector<std::vector<std::vector<std::string>>> _save with: _save.push_back(_map); Later I want (if condition) replace my 2D-Vector by the last-saved 2D-Vector in my 3D-Vector and then delete the last-saved 2D-Vector. My code is: std::vector<std::vector<std::string>> map2; int var = _save.size() -1; // tried with _save.back() too map2.push_back(_save[var]); _save.pop_back(); _map.swap(map2); What am I doing wrong? What should it look like? Thank you for your help. Following error: Structure.cpp: In member function ‘void Structure::undo(bool)’: Structure.cpp:69:30: error: no matching function for call to ‘std::vector<std::vector<std::basic_string<char> > >::push_back(__gnu_cxx::__alloc_traits<std::allocator<std::vector<std::vector<std::basic_string<char> > > > >::value_type&)’ map2.push_back(_save[var]); ^ Structure.cpp:69:30: note: candidates are: In file included from /usr/include/c++/4.9/vector:64:0, from Structure.cpp:6: /usr/include/c++/4.9/bits/stl_vector.h:913:7: note: void std::vector<_Tp, _Alloc>::push_back(const value_type&) [with _Tp = std::vector<std::basic_string<char> >; _Alloc = std::allocator<std::vector<std::basic_string<char> > >; std::vector<_Tp, _Alloc>::value_type = std::vector<std::basic_string<char> >] push_back(const value_type& __x) ^ /usr/include/c++/4.9/bits/stl_vector.h:913:7: note: no known conversion for argument 1 from ‘__gnu_cxx::__alloc_traits<std::allocator<std::vector<std::vector<std::basic_string<char> > > > >::value_type {aka std::vector<std::vector<std::basic_string<char> > >}’ to ‘const value_type& {aka const std::vector<std::basic_string<char> >&}’ /usr/include/c++/4.9/bits/stl_vector.h:931:7: note: void std::vector<_Tp, _Alloc>::push_back(std::vector<_Tp, _Alloc>::value_type&&) [with _Tp = std::vector<std::basic_string<char> >; _Alloc = std::allocator<std::vector<std::basic_string<char> > >; std::vector<_Tp, _Alloc>::value_type = std::vector<std::basic_string<char> >] push_back(value_type&& __x) ^ /usr/include/c++/4.9/bits/stl_vector.h:931:7: note: no known conversion for argument 1 from ‘__gnu_cxx::__alloc_traits<std::allocator<std::vector<std::vector<std::basic_string<char> > > > >::value_type {aka std::vector<std::vector<std::basic_string<char> > >}’ to ‘std::vector<std::vector<std::basic_string<char> > >::value_type&& {aka std::vector<std::basic_string<char> >&&}’ Makefile:28: recipe for target 'Structure.o' failed make: *** [Structure.o] Error 1
Invoking Template Function using Random Access Iterator
As part of an MOC, I am implementing the merge_sort subroutine, and just for curiosity's sake, I would like to implement it using a function signature similar to what std::sort does. The code I have so far is as follows. #include <iostream> #include <vector> template <typename T> bool read_number_list(const std::string& filename, std::vector<T>& output_array){ // ... read number list into an array return true; } template<typename RandomAccessIterator> void merge_sort_array (RandomAccessIterator first, RandomAccessIterator last, std::vector<decltype(*first)>& merge_array){ // ... merge routine } template<typename RandomAccessIterator> void sort_array (RandomAccessIterator first, RandomAccessIterator last){ std::vector<decltype(*first)> merge_array; merge_array.assign(first, last); merge_sort_array(first, last, merge_array); } int main(){ std::vector<int> number_array; read_number_list<int>("file.txt", number_array); sort_array(number_array.begin(), number_array.end()); return 0; } I am seeing errors in the step where I make a copy of the array to be sorted, and also at the point where I invoke the merge_sort_array method. As is customary in templated code, I get the following plethora of errors (which I find quite inscrutable): In file included from /usr/include/x86_64-linux-gnu/c++/4.8/bits/c++allocator.h:33:0, from /usr/include/c++/4.8/bits/allocator.h:46, from /usr/include/c++/4.8/string:41, from /usr/include/c++/4.8/random:41, from /usr/include/c++/4.8/bits/stl_algo.h:65, from /usr/include/c++/4.8/algorithm:62, from /home/balajeerc/Projects/algorithms/src/main/cpp/lib/algorithms/sorting/merge_sorter.h:8, from /home/balajeerc/Projects/algorithms/src/test/cpp/lib/algorithms/sorting/test_inversion_count.cc:1: /usr/include/c++/4.8/ext/new_allocator.h: In instantiation of 'class __gnu_cxx::new_allocator<int&>': /usr/include/c++/4.8/bits/allocator.h:92:11: required from 'class std::allocator<int&>' /usr/include/c++/4.8/bits/alloc_traits.h:90:43: required from 'struct std::allocator_traits<std::allocator<int&> >' /usr/include/c++/4.8/ext/alloc_traits.h:121:10: required from 'struct __gnu_cxx::__alloc_traits<std::allocator<int&> >' /usr/include/c++/4.8/bits/stl_vector.h:75:28: required from 'struct std::_Vector_base<int&, std::allocator<int&> >' /usr/include/c++/4.8/bits/stl_vector.h:210:11: required from 'class std::vector<int&, std::allocator<int&> >' /home/balajeerc/Projects/algorithms/src/test/cpp/lib/algorithms/sorting/test_inversion_count.cc:20:35: required from 'void sort_array(RandomAccessIterator, RandomAccessIterator) [with RandomAccessIterator = __gnu_cxx::__normal_iterator<int*, std::vector<int> >]' /home/balajeerc/Projects/algorithms/src/test/cpp/lib/algorithms/sorting/test_inversion_count.cc:34:56: required from here /usr/include/c++/4.8/ext/new_allocator.h:63:26: error: forming pointer to reference type 'int&' typedef _Tp* pointer; ^ /usr/include/c++/4.8/ext/new_allocator.h:64:26: error: forming pointer to reference type 'int&' typedef const _Tp* const_pointer; ^ In file included from /usr/include/c++/4.8/string:41:0, from /usr/include/c++/4.8/random:41, from /usr/include/c++/4.8/bits/stl_algo.h:65, from /usr/include/c++/4.8/algorithm:62, from /home/balajeerc/Projects/algorithms/src/main/cpp/lib/algorithms/sorting/merge_sorter.h:8, from /home/balajeerc/Projects/algorithms/src/test/cpp/lib/algorithms/sorting/test_inversion_count.cc:1: /usr/include/c++/4.8/bits/allocator.h: In instantiation of 'class std::allocator<int&>': /usr/include/c++/4.8/bits/alloc_traits.h:90:43: required from 'struct std::allocator_traits<std::allocator<int&> >' /usr/include/c++/4.8/ext/alloc_traits.h:121:10: required from 'struct __gnu_cxx::__alloc_traits<std::allocator<int&> >' /usr/include/c++/4.8/bits/stl_vector.h:75:28: required from 'struct std::_Vector_base<int&, std::allocator<int&> >' /usr/include/c++/4.8/bits/stl_vector.h:210:11: required from 'class std::vector<int&, std::allocator<int&> >' /home/balajeerc/Projects/algorithms/src/test/cpp/lib/algorithms/sorting/test_inversion_count.cc:20:35: required from 'void sort_array(RandomAccessIterator, RandomAccessIterator) [with RandomAccessIterator = __gnu_cxx::__normal_iterator<int*, std::vector<int> >]' /home/balajeerc/Projects/algorithms/src/test/cpp/lib/algorithms/sorting/test_inversion_count.cc:34:56: required from here /usr/include/c++/4.8/bits/allocator.h:97:26: error: forming pointer to reference type 'int&' typedef _Tp* pointer; ^ /usr/include/c++/4.8/bits/allocator.h:98:26: error: forming pointer to reference type 'int&' typedef const _Tp* const_pointer; ^ In file included from /usr/include/c++/4.8/ext/alloc_traits.h:36:0, from /usr/include/c++/4.8/bits/stl_construct.h:61, from /usr/include/c++/4.8/bits/stl_tempbuf.h:60, from /usr/include/c++/4.8/bits/stl_algo.h:62, from /usr/include/c++/4.8/algorithm:62, from /home/balajeerc/Projects/algorithms/src/main/cpp/lib/algorithms/sorting/merge_sorter.h:8, from /home/balajeerc/Projects/algorithms/src/test/cpp/lib/algorithms/sorting/test_inversion_count.cc:1: /usr/include/c++/4.8/bits/alloc_traits.h: In instantiation of 'struct std::allocator_traits<std::allocator<int&> >': /usr/include/c++/4.8/ext/alloc_traits.h:121:10: required from 'struct __gnu_cxx::__alloc_traits<std::allocator<int&> >' /usr/include/c++/4.8/bits/stl_vector.h:75:28: required from 'struct std::_Vector_base<int&, std::allocator<int&> >' /usr/include/c++/4.8/bits/stl_vector.h:210:11: required from 'class std::vector<int&, std::allocator<int&> >' /home/balajeerc/Projects/algorithms/src/test/cpp/lib/algorithms/sorting/test_inversion_count.cc:20:35: required from 'void sort_array(RandomAccessIterator, RandomAccessIterator) [with RandomAccessIterator = __gnu_cxx::__normal_iterator<int*, std::vector<int> >]' /home/balajeerc/Projects/algorithms/src/test/cpp/lib/algorithms/sorting/test_inversion_count.cc:34:56: required from here /usr/include/c++/4.8/bits/alloc_traits.h:100:1: error: forming pointer to reference type 'std::allocator_traits<std::allocator<int&> >::value_type {aka int&}' _GLIBCXX_ALLOC_TR_NESTED_TYPE(pointer, value_type*) ^ /usr/include/c++/4.8/bits/alloc_traits.h:100:1: error: no matching function for call to 'std::allocator_traits<std::allocator<int&> >::_S_pointer_helper(std::allocator<int&>*)' _GLIBCXX_ALLOC_TR_NESTED_TYPE(pointer, value_type*) ^ /usr/include/c++/4.8/bits/alloc_traits.h:100:1: note: candidate is: /usr/include/c++/4.8/bits/alloc_traits.h:100:1: note: template<class _Tp> static typename _Tp::pointer std::allocator_traits<_Alloc>::_S_pointer_helper(_Tp*) [with _Tp = _Tp; _Alloc = std::allocator<int&>] _GLIBCXX_ALLOC_TR_NESTED_TYPE(pointer, value_type*) ^ /usr/include/c++/4.8/bits/alloc_traits.h:100:1: note: template argument deduction/substitution failed: /usr/include/c++/4.8/bits/alloc_traits.h:109:1: error: no matching function for call to 'std::allocator_traits<std::allocator<int&> >::_S_const_pointer_helper(std::allocator<int&>*)' _GLIBCXX_ALLOC_TR_NESTED_TYPE(const_pointer, ^ /usr/include/c++/4.8/bits/alloc_traits.h:109:1: note: candidate is: /usr/include/c++/4.8/bits/alloc_traits.h:109:1: note: template<class _Tp> static typename _Tp::const_pointer std::allocator_traits<_Alloc>::_S_const_pointer_helper(_Tp*) [with _Tp = _Tp; _Alloc = std::allocator<int&>] _GLIBCXX_ALLOC_TR_NESTED_TYPE(const_pointer, ^ /usr/include/c++/4.8/bits/alloc_traits.h:109:1: note: template argument deduction/substitution failed: /usr/include/c++/4.8/bits/alloc_traits.h:120:1: error: no matching function for call to 'std::allocator_traits<std::allocator<int&> >::_S_void_pointer_helper(std::allocator<int&>*)' _GLIBCXX_ALLOC_TR_NESTED_TYPE(void_pointer, ^ /usr/include/c++/4.8/bits/alloc_traits.h:120:1: note: candidate is: /usr/include/c++/4.8/bits/alloc_traits.h:120:1: note: template<class _Tp> static typename _Tp::void_pointer std::allocator_traits<_Alloc>::_S_void_pointer_helper(_Tp*) [with _Tp = _Tp; _Alloc = std::allocator<int&>] _GLIBCXX_ALLOC_TR_NESTED_TYPE(void_pointer, ^ /usr/include/c++/4.8/bits/alloc_traits.h:120:1: note: template argument deduction/substitution failed: /usr/include/c++/4.8/bits/alloc_traits.h: In substitution of 'template<class _Tp> static typename _Tp::void_pointer std::allocator_traits<_Alloc>::_S_void_pointer_helper(_Tp*) [with _Tp = _Tp; _Alloc = std::allocator<int&>] [with _Tp = std::allocator<int&>]': /usr/include/c++/4.8/bits/alloc_traits.h:120:1: required from 'struct std::allocator_traits<std::allocator<int&> >' /usr/include/c++/4.8/ext/alloc_traits.h:121:10: required from 'struct __gnu_cxx::__alloc_traits<std::allocator<int&> >' /usr/include/c++/4.8/bits/stl_vector.h:75:28: required from 'struct std::_Vector_base<int&, std::allocator<int&> >' /usr/include/c++/4.8/bits/stl_vector.h:210:11: required from 'class std::vector<int&, std::allocator<int&> >' /home/balajeerc/Projects/algorithms/src/test/cpp/lib/algorithms/sorting/test_inversion_count.cc:20:35: required from 'void sort_array(RandomAccessIterator, RandomAccessIterator) [with RandomAccessIterator = __gnu_cxx::__normal_iterator<int*, std::vector<int> >]' /home/balajeerc/Projects/algorithms/src/test/cpp/lib/algorithms/sorting/test_inversion_count.cc:34:56: required from here /usr/include/c++/4.8/bits/alloc_traits.h:120:1: error: no type named 'void_pointer' in 'class std::allocator<int&>' /usr/include/c++/4.8/bits/alloc_traits.h: In instantiation of 'struct std::allocator_traits<std::allocator<int&> >': /usr/include/c++/4.8/ext/alloc_traits.h:121:10: required from 'struct __gnu_cxx::__alloc_traits<std::allocator<int&> >' /usr/include/c++/4.8/bits/stl_vector.h:75:28: required from 'struct std::_Vector_base<int&, std::allocator<int&> >' /usr/include/c++/4.8/bits/stl_vector.h:210:11: required from 'class std::vector<int&, std::allocator<int&> >' /home/balajeerc/Projects/algorithms/src/test/cpp/lib/algorithms/sorting/test_inversion_count.cc:20:35: required from 'void sort_array(RandomAccessIterator, RandomAccessIterator) [with RandomAccessIterator = __gnu_cxx::__normal_iterator<int*, std::vector<int> >]' /home/balajeerc/Projects/algorithms/src/test/cpp/lib/algorithms/sorting/test_inversion_count.cc:34:56: required from here /usr/include/c++/4.8/bits/alloc_traits.h:131:1: error: no matching function for call to 'std::allocator_traits<std::allocator<int&> >::_S_const_void_pointer_helper(std::allocator<int&>*)' _GLIBCXX_ALLOC_TR_NESTED_TYPE(const_void_pointer, ^ /usr/include/c++/4.8/bits/alloc_traits.h:131:1: note: candidate is: /usr/include/c++/4.8/bits/alloc_traits.h:131:1: note: template<class _Tp> static typename _Tp::const_void_pointer std::allocator_traits<_Alloc>::_S_const_void_pointer_helper(_Tp*) [with _Tp = _Tp; _Alloc = std::allocator<int&>] _GLIBCXX_ALLOC_TR_NESTED_TYPE(const_void_pointer, ^ /usr/include/c++/4.8/bits/alloc_traits.h:131:1: note: template argument deduction/substitution failed: /usr/include/c++/4.8/bits/alloc_traits.h: In substitution of 'template<class _Tp> static typename _Tp::const_void_pointer std::allocator_traits<_Alloc>::_S_const_void_pointer_helper(_Tp*) [with _Tp = _Tp; _Alloc = std::allocator<int&>] [with _Tp = std::allocator<int&>]': /usr/include/c++/4.8/bits/alloc_traits.h:131:1: required from 'struct std::allocator_traits<std::allocator<int&> >' /usr/include/c++/4.8/ext/alloc_traits.h:121:10: required from 'struct __gnu_cxx::__alloc_traits<std::allocator<int&> >' /usr/include/c++/4.8/bits/stl_vector.h:75:28: required from 'struct std::_Vector_base<int&, std::allocator<int&> >' /usr/include/c++/4.8/bits/stl_vector.h:210:11: required from 'class std::vector<int&, std::allocator<int&> >' /home/balajeerc/Projects/algorithms/src/test/cpp/lib/algorithms/sorting/test_inversion_count.cc:20:35: required from 'void sort_array(RandomAccessIterator, RandomAccessIterator) [with RandomAccessIterator = __gnu_cxx::__normal_iterator<int*, std::vector<int> >]' /home/balajeerc/Projects/algorithms/src/test/cpp/lib/algorithms/sorting/test_inversion_count.cc:34:56: required from here /usr/include/c++/4.8/bits/alloc_traits.h:131:1: error: no type named 'const_void_pointer' in 'class std::allocator<int&>' In file included from /usr/include/c++/4.8/bits/stl_construct.h:61:0, from /usr/include/c++/4.8/bits/stl_tempbuf.h:60, from /usr/include/c++/4.8/bits/stl_algo.h:62, from /usr/include/c++/4.8/algorithm:62, from /home/balajeerc/Projects/algorithms/src/main/cpp/lib/algorithms/sorting/merge_sorter.h:8, from /home/balajeerc/Projects/algorithms/src/test/cpp/lib/algorithms/sorting/test_inversion_count.cc:1: /usr/include/c++/4.8/ext/alloc_traits.h: In instantiation of 'struct __gnu_cxx::__alloc_traits<std::allocator<int&> >': /usr/include/c++/4.8/bits/stl_vector.h:75:28: required from 'struct std::_Vector_base<int&, std::allocator<int&> >' /usr/include/c++/4.8/bits/stl_vector.h:210:11: required from 'class std::vector<int&, std::allocator<int&> >' /home/balajeerc/Projects/algorithms/src/test/cpp/lib/algorithms/sorting/test_inversion_count.cc:20:35: required from 'void sort_array(RandomAccessIterator, RandomAccessIterator) [with RandomAccessIterator = __gnu_cxx::__normal_iterator<int*, std::vector<int> >]' /home/balajeerc/Projects/algorithms/src/test/cpp/lib/algorithms/sorting/test_inversion_count.cc:34:56: required from here /usr/include/c++/4.8/ext/alloc_traits.h:137:23: error: no members matching '__gnu_cxx::__alloc_traits<std::allocator<int&> >::_Base_type {aka std::allocator_traits<std::allocator<int&> >}::allocate' in '__gnu_cxx::__alloc_traits<std::allocator<int&> >::_Base_type {aka struct std::allocator_traits<std::allocator<int&> >}' using _Base_type::allocate; ^ /usr/include/c++/4.8/ext/alloc_traits.h:138:23: error: no members matching '__gnu_cxx::__alloc_traits<std::allocator<int&> >::_Base_type {aka std::allocator_traits<std::allocator<int&> >}::deallocate' in '__gnu_cxx::__alloc_traits<std::allocator<int&> >::_Base_type {aka struct std::allocator_traits<std::allocator<int&> >}' using _Base_type::deallocate; ^ In file included from /usr/include/c++/4.8/vector:64:0, from /usr/include/c++/4.8/bits/random.h:34, from /usr/include/c++/4.8/random:50, from /usr/include/c++/4.8/bits/stl_algo.h:65, from /usr/include/c++/4.8/algorithm:62, from /home/balajeerc/Projects/algorithms/src/main/cpp/lib/algorithms/sorting/merge_sorter.h:8, from /home/balajeerc/Projects/algorithms/src/test/cpp/lib/algorithms/sorting/test_inversion_count.cc:1: /usr/include/c++/4.8/bits/stl_vector.h: In instantiation of 'class std::vector<int&, std::allocator<int&> >': /home/balajeerc/Projects/algorithms/src/test/cpp/lib/algorithms/sorting/test_inversion_count.cc:20:35: required from 'void sort_array(RandomAccessIterator, RandomAccessIterator) [with RandomAccessIterator = __gnu_cxx::__normal_iterator<int*, std::vector<int> >]' /home/balajeerc/Projects/algorithms/src/test/cpp/lib/algorithms/sorting/test_inversion_count.cc:34:56: required from here /usr/include/c++/4.8/bits/stl_vector.h:237:20: error: no members matching 'std::vector<int&, std::allocator<int&> >::_Base {aka std::_Vector_base<int&, std::allocator<int&> >}::_M_allocate' in 'std::vector<int&, std::allocator<int&> >::_Base {aka struct std::_Vector_base<int&, std::allocator<int&> >}' using _Base::_M_allocate; ^ /usr/include/c++/4.8/bits/stl_vector.h:238:20: error: no members matching 'std::vector<int&, std::allocator<int&> >::_Base {aka std::_Vector_base<int&, std::allocator<int&> >}::_M_deallocate' in 'std::vector<int&, std::allocator<int&> >::_Base {aka struct std::_Vector_base<int&, std::allocator<int&> >}' using _Base::_M_deallocate; ^ /usr/include/c++/4.8/bits/stl_vector.h:878:7: error: forming pointer to reference type 'int&' data() _GLIBCXX_NOEXCEPT ^ /usr/include/c++/4.8/bits/stl_vector.h:886:7: error: forming pointer to reference type 'int&' data() const _GLIBCXX_NOEXCEPT ^ /usr/include/c++/4.8/bits/stl_vector.h:919:7: error: 'void std::vector<_Tp, _Alloc>::push_back(std::vector<_Tp, _Alloc>::value_type&&) [with _Tp = int&; _Alloc = std::allocator<int&>; std::vector<_Tp, _Alloc>::value_type = int&]' cannot be overloaded push_back(value_type&& __x) ^ /usr/include/c++/4.8/bits/stl_vector.h:901:7: error: with 'void std::vector<_Tp, _Alloc>::push_back(const value_type&) [with _Tp = int&; _Alloc = std::allocator<int&>; std::vector<_Tp, _Alloc>::value_type = int&]' push_back(const value_type& __x) ^ /home/balajeerc/Projects/algorithms/src/test/cpp/lib/algorithms/sorting/test_inversion_count.cc: In instantiation of 'void sort_array(RandomAccessIterator, RandomAccessIterator) [with RandomAccessIterator = __gnu_cxx::__normal_iterator<int*, std::vector<int> >]': /home/balajeerc/Projects/algorithms/src/test/cpp/lib/algorithms/sorting/test_inversion_count.cc:34:56: required from here /home/balajeerc/Projects/algorithms/src/test/cpp/lib/algorithms/sorting/test_inversion_count.cc:22:46: error: 'merge_sort_array' was not declared in this scope, and no declarations were found by argument-dependent lookup at the point of instantiation [-fpermissive] merge_sort_array(first, last, merge_array); ^ /home/balajeerc/Projects/algorithms/src/test/cpp/lib/algorithms/sorting/test_inversion_count.cc:26:6: note: 'template<class RandomAccessIterator> void merge_sort_array(RandomAccessIterator, RandomAccessIterator, std::vector<decltype (* first)>&)' declared here, later in the translation unit void merge_sort_array (RandomAccessIterator first, RandomAccessIterator last, std::vector<decltype(*first)>& merge_array){ ^ make[2]: *** [src/test/cpp/lib/algorithms/sorting/CMakeFiles/test_inversion_count.dir/test_inversion_count.cc.o] Error 1 make[1]: *** [src/test/cpp/lib/algorithms/sorting/CMakeFiles/test_inversion_count.dir/all] Error 2 make: *** [all] Error 2 What am I doing wrong?
Your problem is the misuse of decltype: decltype( expression ) yields T& for lvalue expressions of type T, and you cannot have an std::vector<int&>. Thus the error. To fix that, use typename std::iterator_traits<RandomAccessIterator>::value_type as template parameter instead.
Adding this instance variable to the header of a C++11 file drives the compiler up the wall. Why?
I've been trying to get my artificial intelligence program (written in C++11) to stop spewing out error messages larger than my Terminal will record. I've withered away methods one at a time until the message went away, and have hence found the precise line which makes the computer go bonkers. Here's the pertinent part of my code: #include <iostream> #include <vector> #include <algorithm> #include <math.h> class H { public: H(int); int get_val(); private: int val; }; class Hvote { public: Hvote() : error(1.0) { } std::vector<H&> hs; double error; }; int main() { Hvote hvote; return 0; } To me, it all looks reasonable. However, the compiler (g++) disagrees. The error messages are so long that my Terminal doesn't even bother saving the beginning, so I don't know their true length. However, they're pretty repetitive. The last page, for instance, reads: /usr/include/c++/4.8/bits/stl_vector.h: In instantiation of ‘class std::vector<H&>’: hvote.h:16:25: required from here /usr/include/c++/4.8/bits/stl_vector.h:237:20: error: no members matching ‘std::vector<H&>::_Base {aka std::_Vector_base<H&, std::allocator<H&> >}::_M_allocate’ in ‘std::vector<H&>::_Base {aka struct std::_Vector_base<H&, std::allocator<H&> >}’ using _Base::_M_allocate; ^ /usr/include/c++/4.8/bits/stl_vector.h:238:20: error: no members matching ‘std::vector<H&>::_Base {aka std::_Vector_base<H&, std::allocator<H&> >}::_M_deallocate’ in ‘std::vector<H&>::_Base {aka struct std::_Vector_base<H&, std::allocator<H&> >}’ using _Base::_M_deallocate; ^ /usr/include/c++/4.8/bits/stl_vector.h:878:7: error: forming pointer to reference type ‘H&’ data() _GLIBCXX_NOEXCEPT ^ /usr/include/c++/4.8/bits/stl_vector.h:886:7: error: forming pointer to reference type ‘H&’ data() const _GLIBCXX_NOEXCEPT ^ /usr/include/c++/4.8/bits/stl_vector.h:919:7: error: ‘void std::vector<_Tp, _Alloc>::push_back(std::vector<_Tp, _Alloc>::value_type&&) [with _Tp = H&; _Alloc = std::allocator<H&>; std::vector<_Tp, _Alloc>::value_type = H&]’ cannot be overloaded push_back(value_type&& __x) ^ /usr/include/c++/4.8/bits/stl_vector.h:901:7: error: with ‘void std::vector<_Tp, _Alloc>::push_back(const value_type&) [with _Tp = H&; _Alloc = std::allocator<H&>; std::vector<_Tp, _Alloc>::value_type = H&]’ push_back(const value_type& __x) ^ /usr/include/c++/4.8/bits/stl_vector.h: In instantiation of ‘std::vector<_Tp, _Alloc>::~vector() [with _Tp = H&; _Alloc = std::allocator<H&>]’: hvote.h:10:7: required from here /usr/include/c++/4.8/bits/stl_vector.h:416:30: error: ‘struct std::_Vector_base<H&, std::allocator<H&> >::_Vector_impl’ has no member named ‘_M_start’ _M_get_Tp_allocator()); } ^ /usr/include/c++/4.8/bits/stl_vector.h:416:30: error: ‘struct std::_Vector_base<H&, std::allocator<H&> >::_Vector_impl’ has no member named ‘_M_finish’ /usr/include/c++/4.8/bits/stl_vector.h: In instantiation of ‘std::_Vector_base<_Tp, _Alloc>::~_Vector_base() [with _Tp = H&; _Alloc = std::allocator<H&>]’: /usr/include/c++/4.8/bits/stl_vector.h:416:33: required from ‘std::vector<_Tp, _Alloc>::~vector() [with _Tp = H&; _Alloc = std::allocator<H&>]’ hvote.h:10:7: required from here /usr/include/c++/4.8/bits/stl_vector.h:161:33: error: ‘struct std::_Vector_base<H&, std::allocator<H&> >::_Vector_impl’ has no member named ‘_M_start’ - this->_M_impl._M_start); } ^ /usr/include/c++/4.8/bits/stl_vector.h:161:9: error: ‘struct std::_Vector_base<H&, std::allocator<H&> >::_Vector_impl’ has no member named ‘_M_start’ - this->_M_impl._M_start); } ^ /usr/include/c++/4.8/bits/stl_vector.h:161:9: error: ‘struct std::_Vector_base<H&, std::allocator<H&> >::_Vector_impl’ has no member named ‘_M_end_of_storage’ /usr/include/c++/4.8/bits/stl_vector.h:161:33: error: ‘_M_deallocate’ was not declared in this scope - this->_M_impl._M_start); } ^ However, there's only one little change I need to make so that this all goes away. In hvote.h, just delete the line, std::vector<H&> hs;. Now everything works perfectly! What's the problem with that line? (In my not-reduced and much longer original program, hs is a variable I need, as opposed to here where I've paired away all the methods of hvote that use it) Thanks!
It has a problem with you keeping a vector of non-const references std::vector<H&> hs; I would suggest maintaining either a vector of values or pointers, depending on if you want to own these H instances or just reference them. std::vector<H> hs; std::vector<H*> hs;