Invoking Template Function using Random Access Iterator - c++

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.

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.

C++17 forward declarations of custom structures in not working with custom Hash maps

This is little frustrating me at this point, but I am not sure what am I doing wrong. My code is not compiling for using a hashmap with a custom hash function, which I think should be just okay to compiler.
#include <iostream>
#include <queue>
#include <unordered_map>
struct Key;
struct THash;
struct TEqual;
class Cache {
private:
uint64_t capacity;
uint64_t time_ticker;
std::unordered_map<Key, int32_t, THash, TEqual> map_keys;
std::priority_queue<Key> lru_min_heap;
void RehashPriorityQueue()
{
lru_min_heap = std::priority_queue<Key>();
for (auto &&iter : map_keys) lru_min_heap.emplace(iter.first);
}
public:
Cache(const uint64_t cache_size=1) : capacity(cache_size), time_ticker(0) {}
~Cache(){}
void Set(int32_t key, int32_t value) {
time_ticker++;
auto _key = Key(key, time_ticker);
auto find_key = map_keys.find(_key);
if(capacity) capacity--;
else
{
auto lru = lru_min_heap.top(); lru_min_heap.pop();
auto find_key_lru = map_keys.find(lru);
if(find_key_lru != map_keys.end()) map_keys.erase(find_key_lru);
}
if(find_key != map_keys.end())
{
map_keys.erase(find_key);
RehashPriorityQueue();
}
map_keys.emplace(_key, value);
lru_min_heap.emplace(_key);
}
int32_t Get(int32_t key) {
time_ticker++;
int ret = 0;
auto _key = Key(key, time_ticker);
auto find_key = map_keys.find(_key);
if(find_key != map_keys.end()) {
ret = find_key->second;
map_keys.erase(find_key);
RehashPriorityQueue();
map_keys.emplace(_key, ret);
lru_min_heap.emplace(_key);
}
else
{
std::cout<<"None ";
}
return ret;
}
};
struct Key {
int32_t id;
uint64_t weightage;
Key(int32_t id, uint64_t weightage=1) : id(id), weightage(weightage) {};
bool operator<(const Key &other) const { return weightage > other.weightage;}
size_t operator()(const Key& tr) const {
return std::hash<int32_t>()(tr.id);
}
};
struct THash {
size_t operator()(const Key& tr) const {
return std::hash<int32_t>()(tr.id);
}
};
struct TEqual {
bool operator()(const Key& tr1, const Key& tr2) const {
if (tr1.id == tr2.id) return true;
return false;
}
};
int main()
{
Cache cache(2);
cache.Set(3, 3);
std::cout<<cache.Get(3)<<"\n";
return 0;
}
compiler error:
In file included from /usr/include/c++/4.9/bits/hashtable.h:35:0,
from /usr/include/c++/4.9/unordered_map:47,
from 3:
/usr/include/c++/4.9/bits/hashtable_policy.h: In instantiation of 'struct std::__detail::__is_noexcept_hash<Key, THash>':
/usr/include/c++/4.9/type_traits:134:12: required from 'struct std::__and_<std::__is_fast_hash<THash>, std::__detail::__is_noexcept_hash<Key, THash> >'
/usr/include/c++/4.9/type_traits:145:38: required from 'struct std::__not_<std::__and_<std::__is_fast_hash<THash>, std::__detail::__is_noexcept_hash<Key, THash> > >'
/usr/include/c++/4.9/bits/unordered_map.h:100:66: required from 'class std::unordered_map<Key, int, THash, TEqual>'
13:53: required from here
/usr/include/c++/4.9/bits/hashtable_policy.h:85:33: error: no match for call to '(const THash) (const Key&)'
noexcept(declval<const _Hash&>()(declval<const _Key&>()))>
^
In file included from /usr/include/c++/4.9/bits/move.h:57:0,
from /usr/include/c++/4.9/bits/stl_pair.h:59,
from /usr/include/c++/4.9/bits/stl_algobase.h:64,
from /usr/include/c++/4.9/bits/char_traits.h:39,
from /usr/include/c++/4.9/ios:40,
from /usr/include/c++/4.9/ostream:38,
from /usr/include/c++/4.9/iostream:39,
from 1:
/usr/include/c++/4.9/type_traits: In instantiation of 'struct std::__not_<std::__and_<std::__is_fast_hash<THash>, std::__detail::__is_noexcept_hash<Key, THash> > >':
/usr/include/c++/4.9/bits/unordered_map.h:100:66: required from 'class std::unordered_map<Key, int, THash, TEqual>'
13:53: required from here
/usr/include/c++/4.9/type_traits:145:38: error: 'value' is not a member of 'std::__and_<std::__is_fast_hash<THash>, std::__detail::__is_noexcept_hash<Key, THash> >'
: public integral_constant<bool, !_Pp::value>
^
In file included from /usr/include/c++/4.9/unordered_map:48:0,
from 3:
/usr/include/c++/4.9/bits/unordered_map.h: In instantiation of 'class std::unordered_map<Key, int, THash, TEqual>':
13:53: required from here
/usr/include/c++/4.9/bits/unordered_map.h:100:66: error: 'value' is not a member of 'std::__not_<std::__and_<std::__is_fast_hash<THash>, std::__detail::__is_noexcept_hash<Key, THash> > >'
typedef __umap_hashtable<_Key, _Tp, _Hash, _Pred, _Alloc> _Hashtable;
^
/usr/include/c++/4.9/bits/unordered_map.h:107:45: error: 'value' is not a member of 'std::__not_<std::__and_<std::__is_fast_hash<THash>, std::__detail::__is_noexcept_hash<Key, THash> > >'
typedef typename _Hashtable::key_type key_type;
^
/usr/include/c++/4.9/bits/unordered_map.h:108:47: error: 'value' is not a member of 'std::__not_<std::__and_<std::__is_fast_hash<THash>, std::__detail::__is_noexcept_hash<Key, THash> > >'
typedef typename _Hashtable::value_type value_type;
^
/usr/include/c++/4.9/bits/unordered_map.h:109:48: error: 'value' is not a member of 'std::__not_<std::__and_<std::__is_fast_hash<THash>, std::__detail::__is_noexcept_hash<Key, THash> > >'
typedef typename _Hashtable::mapped_type mapped_type;
^
/usr/include/c++/4.9/bits/unordered_map.h:110:43: error: 'value' is not a member of 'std::__not_<std::__and_<std::__is_fast_hash<THash>, std::__detail::__is_noexcept_hash<Key, THash> > >'
typedef typename _Hashtable::hasher hasher;
^
/usr/include/c++/4.9/bits/unordered_map.h:111:46: error: 'value' is not a member of 'std::__not_<std::__and_<std::__is_fast_hash<THash>, std::__detail::__is_noexcept_hash<Key, THash> > >'
typedef typename _Hashtable::key_equal key_equal;
^
/usr/include/c++/4.9/bits/unordered_map.h:112:51: error: 'value' is not a member of 'std::__not_<std::__and_<std::__is_fast_hash<THash>, std::__detail::__is_noexcept_hash<Key, THash> > >'
typedef typename _Hashtable::allocator_type allocator_type;
^
/usr/include/c++/4.9/bits/unordered_map.h:117:45: error: 'value' is not a member of 'std::__not_<std::__and_<std::__is_fast_hash<THash>, std::__detail::__is_noexcept_hash<Key, THash> > >'
typedef typename _Hashtable::pointer pointer;
^
/usr/include/c++/4.9/bits/unordered_map.h:118:50: error: 'value' is not a member of 'std::__not_<std::__and_<std::__is_fast_hash<THash>, std::__detail::__is_noexcept_hash<Key, THash> > >'
typedef typename _Hashtable::const_pointer const_pointer;
^
/usr/include/c++/4.9/bits/unordered_map.h:119:47: error: 'value' is not a member of 'std::__not_<std::__and_<std::__is_fast_hash<THash>, std::__detail::__is_noexcept_hash<Key, THash> > >'
typedef typename _Hashtable::reference reference;
^
/usr/include/c++/4.9/bits/unordered_map.h:120:52: error: 'value' is not a member of 'std::__not_<std::__and_<std::__is_fast_hash<THash>, std::__detail::__is_noexcept_hash<Key, THash> > >'
typedef typename _Hashtable::const_reference const_reference;
^
/usr/include/c++/4.9/bits/unordered_map.h:121:46: error: 'value' is not a member of 'std::__not_<std::__and_<std::__is_fast_hash<THash>, std::__detail::__is_noexcept_hash<Key, THash> > >'
typedef typename _Hashtable::iterator iterator;
^
/usr/include/c++/4.9/bits/unordered_map.h:122:51: error: 'value' is not a member of 'std::__not_<std::__and_<std::__is_fast_hash<THash>, std::__detail::__is_noexcept_hash<Key, THash> > >'
typedef typename _Hashtable::const_iterator const_iterator;
^
/usr/include/c++/4.9/bits/unordered_map.h:123:51: error: 'value' is not a member of 'std::__not_<std::__and_<std::__is_fast_hash<THash>, std::__detail::__is_noexcept_hash<Key, THash> > >'
typedef typename _Hashtable::local_iterator local_iterator;
^
/usr/include/c++/4.9/bits/unordered_map.h:124:57: error: 'value' is not a member of 'std::__not_<std::__and_<std::__is_fast_hash<THash>, std::__detail::__is_noexcept_hash<Key, THash> > >'
typedef typename _Hashtable::const_local_iterator const_local_iterator;
^
/usr/include/c++/4.9/bits/unordered_map.h:125:47: error: 'value' is not a member of 'std::__not_<std::__and_<std::__is_fast_hash<THash>, std::__detail::__is_noexcept_hash<Key, THash> > >'
typedef typename _Hashtable::size_type size_type;
^
/usr/include/c++/4.9/bits/unordered_map.h:126:52: error: 'value' is not a member of 'std::__not_<std::__and_<std::__is_fast_hash<THash>, std::__detail::__is_noexcept_hash<Key, THash> > >'
typedef typename _Hashtable::difference_type difference_type;
^
/usr/include/c++/4.9/bits/unordered_map.h:242:7: error: 'value' is not a member of 'std::__not_<std::__and_<std::__is_fast_hash<THash>, std::__detail::__is_noexcept_hash<Key, THash> > >'
operator=(initializer_list<value_type> __l)
^
/usr/include/c++/4.9/bits/unordered_map.h:340:2: error: 'value' is not a member of 'std::__not_<std::__and_<std::__is_fast_hash<THash>, std::__detail::__is_noexcept_hash<Key, THash> > >'
emplace(_Args&&... __args)
^
/usr/include/c++/4.9/bits/unordered_map.h:392:7: error: 'value' is not a member of 'std::__not_<std::__and_<std::__is_fast_hash<THash>, std::__detail::__is_noexcept_hash<Key, THash> > >'
insert(const value_type& __x)
^
/usr/include/c++/4.9/bits/unordered_map.h:399:2: error: 'value' is not a member of 'std::__not_<std::__and_<std::__is_fast_hash<THash>, std::__detail::__is_noexcept_hash<Key, THash> > >'
insert(_Pair&& __x)
^
/usr/include/c++/4.9/bits/unordered_map.h:459:7: error: 'value' is not a member of 'std::__not_<std::__and_<std::__is_fast_hash<THash>, std::__detail::__is_noexcept_hash<Key, THash> > >'
insert(initializer_list<value_type> __l)
^
/usr/include/c++/4.9/bits/unordered_map.h:604:7: error: 'value' is not a member of 'std::__not_<std::__and_<std::__is_fast_hash<THash>, std::__detail::__is_noexcept_hash<Key, THash> > >'
equal_range(const key_type& __x)
^
/usr/include/c++/4.9/bits/unordered_map.h:608:7: error: 'value' is not a member of 'std::__not_<std::__and_<std::__is_fast_hash<THash>, std::__detail::__is_noexcept_hash<Key, THash> > >'
equal_range(const key_type& __x) const
^
In file included from /usr/include/c++/4.9/bits/stl_algobase.h:64:0,
from /usr/include/c++/4.9/bits/char_traits.h:39,
from /usr/include/c++/4.9/ios:40,
from /usr/include/c++/4.9/ostream:38,
from /usr/include/c++/4.9/iostream:39,
from 1:
/usr/include/c++/4.9/bits/stl_pair.h: In instantiation of 'struct std::pair<const Key, int>':
19:28: required from here
/usr/include/c++/4.9/bits/stl_pair.h:101:11: error: 'std::pair<_T1, _T2>::first' has incomplete type
_T1 first; /// #c first is a copy of the first object
^
5:8: error: forward declaration of 'const struct Key'
In member function 'void Cache::RehashPriorityQueue()':
19:28: error: no matching function for call to 'begin(std::unordered_map<Key, int, THash, TEqual>&)'
19:28: note: candidates are:
In file included from /usr/include/c++/4.9/bits/basic_string.h:42:0,
from /usr/include/c++/4.9/string:52,
from /usr/include/c++/4.9/bits/locale_classes.h:40,
from /usr/include/c++/4.9/bits/ios_base.h:41,
from /usr/include/c++/4.9/ios:42,
from /usr/include/c++/4.9/ostream:38,
from /usr/include/c++/4.9/iostream:39,
from 1:
/usr/include/c++/4.9/initializer_list:89:5: note: template<class _Tp> constexpr const _Tp* std::begin(std::initializer_list<_Tp>)
begin(initializer_list<_Tp> __ils) noexcept
^
/usr/include/c++/4.9/initializer_list:89:5: note: template argument deduction/substitution failed:
19:28: note: 'std::unordered_map<Key, int, THash, TEqual>' is not derived from 'std::initializer_list<_Tp>'
In file included from /usr/include/c++/4.9/string:51:0,
from /usr/include/c++/4.9/bits/locale_classes.h:40,
from /usr/include/c++/4.9/bits/ios_base.h:41,
from /usr/include/c++/4.9/ios:42,
from /usr/include/c++/4.9/ostream:38,
from /usr/include/c++/4.9/iostream:39,
from 1:
/usr/include/c++/4.9/bits/range_access.h:87:5: note: template<class _Tp, long unsigned int _Nm> _Tp* std::begin(_Tp (&)[_Nm])
begin(_Tp (&__arr)[_Nm])
^
/usr/include/c++/4.9/bits/range_access.h:87:5: note: template argument deduction/substitution failed:
19:28: note: mismatched types '_Tp [_Nm]' and 'std::unordered_map<Key, int, THash, TEqual>'
In file included from /usr/include/c++/4.9/string:51:0,
from /usr/include/c++/4.9/bits/locale_classes.h:40,
from /usr/include/c++/4.9/bits/ios_base.h:41,
from /usr/include/c++/4.9/ios:42,
from /usr/include/c++/4.9/ostream:38,
from /usr/include/c++/4.9/iostream:39,
from 1:
/usr/include/c++/4.9/bits/range_access.h:58:5: note: template<class _Container> decltype (__cont.begin()) std::begin(const _Container&)
begin(const _Container& __cont) -> decltype(__cont.begin())
^
/usr/include/c++/4.9/bits/range_access.h:58:5: note: template argument deduction/substitution failed:
/usr/include/c++/4.9/bits/range_access.h: In substitution of 'template<class _Container> decltype (__cont.begin()) std::begin(const _Container&) [with _Container = std::unordered_map<Key, int, THash, TEqual>]':
19:28: required from here
/usr/include/c++/4.9/bits/range_access.h:58:5: error: 'const class std::unordered_map<Key, int, THash, TEqual>' has no member named 'begin'
/usr/include/c++/4.9/bits/range_access.h:48:5: note: template<class _Container> decltype (__cont.begin()) std::begin(_Container&)
begin(_Container& __cont) -> decltype(__cont.begin())
^
/usr/include/c++/4.9/bits/range_access.h:48:5: note: template argument deduction/substitution failed:
/usr/include/c++/4.9/bits/range_access.h: In substitution of 'template<class _Container> decltype (__cont.begin()) std::begin(_Container&) [with _Container = std::unordered_map<Key, int, THash, TEqual>]':
19:28: required from here
/usr/include/c++/4.9/bits/range_access.h:48:5: error: 'class std::unordered_map<Key, int, THash, TEqual>' has no member named 'begin'
19:28: error: no matching function for call to 'end(std::unordered_map<Key, int, THash, TEqual>&)'
19:28: note: candidates are:
In file included from /usr/include/c++/4.9/bits/basic_string.h:42:0,
from /usr/include/c++/4.9/string:52,
from /usr/include/c++/4.9/bits/locale_classes.h:40,
from /usr/include/c++/4.9/bits/ios_base.h:41,
from /usr/include/c++/4.9/ios:42,
from /usr/include/c++/4.9/ostream:38,
from /usr/include/c++/4.9/iostream:39,
from 1:
/usr/include/c++/4.9/initializer_list:99:5: note: template<class _Tp> constexpr const _Tp* std::end(std::initializer_list<_Tp>)
end(initializer_list<_Tp> __ils) noexcept
^
/usr/include/c++/4.9/initializer_list:99:5: note: template argument deduction/substitution failed:
19:28: note: 'std::unordered_map<Key, int, THash, TEqual>' is not derived from 'std::initializer_list<_Tp>'
In file included from /usr/include/c++/4.9/string:51:0,
from /usr/include/c++/4.9/bits/locale_classes.h:40,
from /usr/include/c++/4.9/bits/ios_base.h:41,
from /usr/include/c++/4.9/ios:42,
from /usr/include/c++/4.9/ostream:38,
from /usr/include/c++/4.9/iostream:39,
from 1:
/usr/include/c++/4.9/bits/range_access.h:97:5: note: template<class _Tp, long unsigned int _Nm> _Tp* std::end(_Tp (&)[_Nm])
end(_Tp (&__arr)[_Nm])
^
/usr/include/c++/4.9/bits/range_access.h:97:5: note: template argument deduction/substitution failed:
19:28: note: mismatched types '_Tp [_Nm]' and 'std::unordered_map<Key, int, THash, TEqual>'
In file included from /usr/include/c++/4.9/string:51:0,
from /usr/include/c++/4.9/bits/locale_classes.h:40,
from /usr/include/c++/4.9/bits/ios_base.h:41,
from /usr/include/c++/4.9/ios:42,
from /usr/include/c++/4.9/ostream:38,
from /usr/include/c++/4.9/iostream:39,
from 1:
/usr/include/c++/4.9/bits/range_access.h:78:5: note: template<class _Container> decltype (__cont.end()) std::end(const _Container&)
end(const _Container& __cont) -> decltype(__cont.end())
^
/usr/include/c++/4.9/bits/range_access.h:78:5: note: template argument deduction/substitution failed:
/usr/include/c++/4.9/bits/range_access.h: In substitution of 'template<class _Container> decltype (__cont.end()) std::end(const _Container&) [with _Container = std::unordered_map<Key, int, THash, TEqual>]':
19:28: required from here
/usr/include/c++/4.9/bits/range_access.h:78:5: error: 'const class std::unordered_map<Key, int, THash, TEqual>' has no member named 'end'
/usr/include/c++/4.9/bits/range_access.h:68:5: note: template<class _Container> decltype (__cont.end()) std::end(_Container&)
end(_Container& __cont) -> decltype(__cont.end())
^
/usr/include/c++/4.9/bits/range_access.h:68:5: note: template argument deduction/substitution failed:
/usr/include/c++/4.9/bits/range_access.h: In substitution of 'template<class _Container> decltype (__cont.end()) std::end(_Container&) [with _Container = std::unordered_map<Key, int, THash, TEqual>]':
19:28: required from here
/usr/include/c++/4.9/bits/range_access.h:68:5: error: 'class std::unordered_map<Key, int, THash, TEqual>' has no member named 'end'
In constructor 'Cache::Cache(uint64_t)':
23:77: error: no matching function for call to 'std::unordered_map<Key, int, THash, TEqual>::unordered_map()'
23:77: note: candidates are:
In file included from /usr/include/c++/4.9/unordered_map:48:0,
from 3:
/usr/include/c++/4.9/bits/unordered_map.h:172:7: note: std::unordered_map<_Key, _Tp, _Hash, _Pred, _Alloc>::unordered_map(std::unordered_map<_Key, _Tp, _Hash, _Pred, _Alloc>&&) [with _Key = Key; _Tp = int; _Hash = THash; _Pred = TEqual; _Alloc = std::allocator<std::pair<const Key, int> >]
unordered_map(unordered_map&&) = default;
^
/usr/include/c++/4.9/bits/unordered_map.h:172:7: note: candidate expects 1 argument, 0 provided
/usr/include/c++/4.9/bits/unordered_map.h:169:7: note: std::unordered_map<_Key, _Tp, _Hash, _Pred, _Alloc>::unordered_map(const std::unordered_map<_Key, _Tp, _Hash, _Pred, _Alloc>&) [with _Key = Key; _Tp = int; _Hash = THash; _Pred = TEqual; _Alloc = std::allocator<std::pair<const Key, int> >]
unordered_map(const unordered_map&) = default;
^
/usr/include/c++/4.9/bits/unordered_map.h:169:7: note: candidate expects 1 argument, 0 provided
In member function 'void Cache::Set(int32_t, int32_t)':
28:41: error: invalid use of incomplete type 'struct Key'
5:8: error: forward declaration of 'struct Key'
29:34: error: 'class std::unordered_map<Key, int, THash, TEqual>' has no member named 'find'
34:41: error: 'Key lru' has incomplete type
35:42: error: 'class std::unordered_map<Key, int, THash, TEqual>' has no member named 'find'
36:41: error: 'class std::unordered_map<Key, int, THash, TEqual>' has no member named 'end'
36:57: error: 'class std::unordered_map<Key, int, THash, TEqual>' has no member named 'erase'
39:33: error: 'class std::unordered_map<Key, int, THash, TEqual>' has no member named 'end'
41:22: error: 'class std::unordered_map<Key, int, THash, TEqual>' has no member named 'erase'
45:18: error: 'class std::unordered_map<Key, int, THash, TEqual>' has no member named 'emplace'
In member function 'int32_t Cache::Get(int32_t)':
52:41: error: invalid use of incomplete type 'struct Key'
5:8: error: forward declaration of 'struct Key'
53:34: error: 'class std::unordered_map<Key, int, THash, TEqual>' has no member named 'find'
54:33: error: 'class std::unordered_map<Key, int, THash, TEqual>' has no member named 'end'
56:22: error: 'class std::unordered_map<Key, int, THash, TEqual>' has no member named 'erase'
58:22: error: 'class std::unordered_map<Key, int, THash, TEqual>' has no member named 'emplace'

Eigen ref with child matrix class

I've got the following code:
#include <iostream>
#include <eigen3/Eigen/Dense>
class MyVectorType: public Eigen::Matrix<double, 3, 1> {
public:
MyVectorType(void) :
Eigen::Matrix<double, 3, 1>() {
}
typedef Eigen::Matrix<double, 3, 1> Base;
// This constructor allows you to construct MyVectorType from Eigen expressions
template<typename OtherDerived>
MyVectorType(const Eigen::MatrixBase<OtherDerived>& other) :
Eigen::Matrix<double, 3, 1>(other) {
}
// This method allows you to assign Eigen expressions to MyVectorType
template<typename OtherDerived>
MyVectorType & operator=(const Eigen::MatrixBase<OtherDerived>& other) {
this->Base::operator=(other);
return *this;
}
//other custom methods here
};
void init(Eigen::Ref<MyVectorType>& m) {
for (int i = 0; i < 3; i++)
for (int j = 0; j < 1; j++)
m(i, j) = 1;
}
int main() {
Eigen::Matrix<double, Eigen::Dynamic, Eigen::Dynamic, Eigen::AutoAlign, 12,
12> mm(3, 1);
Eigen::Ref<MyVectorType> rr(mm);
init(rr);
std::cout << mm << std::endl;
return 0;
}
I defined my own class but when I try to the Ref class I have the following error from gcc:
make all
Building file: ../main.cpp
Invoking: GCC C++ Compiler
g++ -std=c++98 -O0 -g3 -Wall -c -fmessage-length=0 -MMD -MP -MF"main.d" -MT"main.o" -o "main.o" "../main.cpp"
In file included from /usr/include/eigen3/Eigen/Core:449:0,
from /usr/include/eigen3/Eigen/Dense:1,
from ../main.cpp:2:
/usr/include/eigen3/Eigen/src/Core/Map.h: In instantiation of ‘struct Eigen::internal::traits<Eigen::Map<MyVectorType, 0, Eigen::OuterStride<> > >’:
/usr/include/eigen3/Eigen/src/Core/Ref.h:18:8: required from ‘struct Eigen::internal::traits<Eigen::Ref<MyVectorType> >’
/usr/include/eigen3/Eigen/src/Core/util/ForwardDeclarations.h:32:54: required from ‘struct Eigen::internal::accessors_level<Eigen::Ref<MyVectorType> >’
/usr/include/eigen3/Eigen/src/Core/util/ForwardDeclarations.h:113:75: required from ‘class Eigen::RefBase<Eigen::Ref<MyVectorType> >’
/usr/include/eigen3/Eigen/src/Core/Ref.h:190:76: required from ‘class Eigen::Ref<MyVectorType>’
../main.cpp:26:10: required from here
/usr/include/eigen3/Eigen/src/Core/Map.h:18:8: error: invalid use of incomplete type ‘struct Eigen::internal::traits<MyVectorType>’
struct traits<Map<PlainObjectType, MapOptions, StrideType> >
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
In file included from /usr/include/eigen3/Eigen/Core:346:0,
from /usr/include/eigen3/Eigen/Dense:1,
from ../main.cpp:2:
/usr/include/eigen3/Eigen/src/Core/util/ForwardDeclarations.h:17:29: note: declaration of ‘struct Eigen::internal::traits<MyVectorType>’
template<typename T> struct traits;
^~~~~~
In file included from /usr/include/eigen3/Eigen/Core:449:0,
from /usr/include/eigen3/Eigen/Dense:1,
from ../main.cpp:2:
/usr/include/eigen3/Eigen/src/Core/Map.h:30:32: error: incomplete type ‘Eigen::internal::traits<Eigen::Map<MyVectorType, 0, Eigen::OuterStride<> > >::TraitsBase {aka Eigen::internal::traits<MyVectorType>}’ used in nested name specifier
Flags0 = TraitsBase::Flags & (~NestByRefBit),
~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~
In file included from /usr/include/eigen3/Eigen/Core:348:0,
from /usr/include/eigen3/Eigen/Dense:1,
from ../main.cpp:2:
/usr/include/eigen3/Eigen/src/Core/util/XprHelper.h: In instantiation of ‘struct Eigen::internal::is_lvalue<MyVectorType>’:
/usr/include/eigen3/Eigen/src/Core/Map.h:31:47: required from ‘struct Eigen::internal::traits<Eigen::Map<MyVectorType, 0, Eigen::OuterStride<> > >’
/usr/include/eigen3/Eigen/src/Core/Ref.h:18:8: required from ‘struct Eigen::internal::traits<Eigen::Ref<MyVectorType> >’
/usr/include/eigen3/Eigen/src/Core/util/ForwardDeclarations.h:32:54: required from ‘struct Eigen::internal::accessors_level<Eigen::Ref<MyVectorType> >’
/usr/include/eigen3/Eigen/src/Core/util/ForwardDeclarations.h:113:75: required from ‘class Eigen::RefBase<Eigen::Ref<MyVectorType> >’
/usr/include/eigen3/Eigen/src/Core/Ref.h:190:76: required from ‘class Eigen::Ref<MyVectorType>’
../main.cpp:26:10: required from here
/usr/include/eigen3/Eigen/src/Core/util/XprHelper.h:642:53: error: incomplete type ‘Eigen::internal::traits<MyVectorType>’ used in nested name specifier
bool(traits<ExpressionType>::Flags & LvalueBit) };
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~
In file included from /usr/include/eigen3/Eigen/Core:448:0,
from /usr/include/eigen3/Eigen/Dense:1,
from ../main.cpp:2:
/usr/include/eigen3/Eigen/src/Core/MapBase.h: In instantiation of ‘class Eigen::MapBase<Eigen::Ref<MyVectorType>, 0>’:
/usr/include/eigen3/Eigen/src/Core/Ref.h:58:34: required from ‘class Eigen::RefBase<Eigen::Ref<MyVectorType> >’
/usr/include/eigen3/Eigen/src/Core/Ref.h:190:76: required from ‘class Eigen::Ref<MyVectorType>’
../main.cpp:26:10: required from here
/usr/include/eigen3/Eigen/src/Core/MapBase.h:37:34: error: no type named ‘XprKind’ in ‘struct Eigen::internal::traits<Eigen::Ref<MyVectorType> >’
template<typename Derived> class MapBase<Derived, ReadOnlyAccessors>
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/usr/include/eigen3/Eigen/src/Core/MapBase.h:42:62: error: no type named ‘XprKind’ in ‘struct Eigen::internal::traits<Eigen::Ref<MyVectorType> >’
typedef typename internal::dense_xpr_base<Derived>::type Base;
^~~~
/usr/include/eigen3/Eigen/src/Core/MapBase.h:43:10: error: ‘RowsAtCompileTime’ is not a member of ‘Eigen::internal::traits<Eigen::Ref<MyVectorType> >’
enum {
^
/usr/include/eigen3/Eigen/src/Core/MapBase.h:43:10: error: ‘ColsAtCompileTime’ is not a member of ‘Eigen::internal::traits<Eigen::Ref<MyVectorType> >’
/usr/include/eigen3/Eigen/src/Core/MapBase.h:49:61: error: no type named ‘StorageKind’ in ‘struct Eigen::internal::traits<Eigen::Ref<MyVectorType> >’
typedef typename internal::traits<Derived>::StorageKind StorageKind;
^~~~~~~~~~~
/usr/include/eigen3/Eigen/src/Core/MapBase.h:50:56: error: no type named ‘Scalar’ in ‘struct Eigen::internal::traits<Eigen::Ref<MyVectorType> >’
typedef typename internal::traits<Derived>::Scalar Scalar;
^~~~~~
/usr/include/eigen3/Eigen/src/Core/MapBase.h:51:60: error: no type named ‘Scalar’ in ‘struct Eigen::internal::traits<Eigen::Ref<MyVectorType> >’
typedef typename internal::packet_traits<Scalar>::type PacketScalar;
^~~~~~~~~~~~
/usr/include/eigen3/Eigen/src/Core/MapBase.h:52:46: error: no type named ‘Scalar’ in ‘struct Eigen::internal::traits<Eigen::Ref<MyVectorType> >’
typedef typename NumTraits<Scalar>::Real RealScalar;
^~~~~~~~~~
/usr/include/eigen3/Eigen/src/Core/MapBase.h:57:22: error: no type named ‘Scalar’ in ‘struct Eigen::internal::traits<Eigen::Ref<MyVectorType> >’
PointerType;
^~~~~~~~~~~
/usr/include/eigen3/Eigen/src/Core/MapBase.h:59:17: error: using-declaration for non-member at class scope
using Base::derived;
^~~~~~~
/usr/include/eigen3/Eigen/src/Core/MapBase.h:63:17: error: using-declaration for non-member at class scope
using Base::MaxRowsAtCompileTime;
^~~~~~~~~~~~~~~~~~~~
/usr/include/eigen3/Eigen/src/Core/MapBase.h:64:17: error: using-declaration for non-member at class scope
using Base::MaxColsAtCompileTime;
^~~~~~~~~~~~~~~~~~~~
/usr/include/eigen3/Eigen/src/Core/MapBase.h:65:17: error: using-declaration for non-member at class scope
using Base::MaxSizeAtCompileTime;
^~~~~~~~~~~~~~~~~~~~
/usr/include/eigen3/Eigen/src/Core/MapBase.h:66:17: error: using-declaration for non-member at class scope
using Base::IsVectorAtCompileTime;
^~~~~~~~~~~~~~~~~~~~~
/usr/include/eigen3/Eigen/src/Core/MapBase.h:67:17: error: using-declaration for non-member at class scope
using Base::Flags;
^~~~~
/usr/include/eigen3/Eigen/src/Core/MapBase.h:68:17: error: using-declaration for non-member at class scope
using Base::IsRowMajor;
^~~~~~~~~~
/usr/include/eigen3/Eigen/src/Core/MapBase.h:70:17: error: using-declaration for non-member at class scope
using Base::rows;
^~~~
/usr/include/eigen3/Eigen/src/Core/MapBase.h:71:17: error: using-declaration for non-member at class scope
using Base::cols;
^~~~
/usr/include/eigen3/Eigen/src/Core/MapBase.h:72:17: error: using-declaration for non-member at class scope
using Base::size;
^~~~
/usr/include/eigen3/Eigen/src/Core/MapBase.h:73:17: error: using-declaration for non-member at class scope
using Base::coeff;
^~~~~
/usr/include/eigen3/Eigen/src/Core/MapBase.h:74:17: error: using-declaration for non-member at class scope
using Base::coeffRef;
^~~~~~~~
/usr/include/eigen3/Eigen/src/Core/MapBase.h:75:17: error: using-declaration for non-member at class scope
using Base::lazyAssign;
^~~~~~~~~~
/usr/include/eigen3/Eigen/src/Core/MapBase.h:76:17: error: using-declaration for non-member at class scope
using Base::eval;
^~~~
/usr/include/eigen3/Eigen/src/Core/MapBase.h:78:17: error: using-declaration for non-member at class scope
using Base::innerStride;
^~~~~~~~~~~
/usr/include/eigen3/Eigen/src/Core/MapBase.h:79:17: error: using-declaration for non-member at class scope
using Base::outerStride;
^~~~~~~~~~~
/usr/include/eigen3/Eigen/src/Core/MapBase.h:80:17: error: using-declaration for non-member at class scope
using Base::rowStride;
^~~~~~~~~
/usr/include/eigen3/Eigen/src/Core/MapBase.h:81:17: error: using-declaration for non-member at class scope
using Base::colStride;
^~~~~~~~~
/usr/include/eigen3/Eigen/src/Core/MapBase.h:84:25: error: using-declaration for non-member at class scope
using Base::operator=;
^
In file included from /usr/include/eigen3/Eigen/Core:72:0,
from /usr/include/eigen3/Eigen/Dense:1,
from ../main.cpp:2:
/usr/include/eigen3/Eigen/src/Core/Ref.h: In instantiation of ‘class Eigen::RefBase<Eigen::Ref<MyVectorType> >’:
/usr/include/eigen3/Eigen/src/Core/Ref.h:190:76: required from ‘class Eigen::Ref<MyVectorType>’
../main.cpp:26:10: required from here
/usr/include/eigen3/Eigen/src/Core/Ref.h:67:3: error: no type named ‘Scalar’ in ‘struct Eigen::internal::traits<Eigen::RefBase<Eigen::Ref<MyVectorType> > >’
EIGEN_DENSE_PUBLIC_INTERFACE(RefBase)
^
/usr/include/eigen3/Eigen/src/Core/Ref.h:67:3: error: no type named ‘Scalar’ in ‘struct Eigen::internal::traits<Eigen::RefBase<Eigen::Ref<MyVectorType> > >’
EIGEN_DENSE_PUBLIC_INTERFACE(RefBase)
^
In file included from /usr/include/eigen3/Eigen/Core:72:0,
from /usr/include/eigen3/Eigen/Dense:1,
from ../main.cpp:2:
/usr/include/eigen3/Eigen/src/Core/Ref.h:67:3: error: no type named ‘StorageKind’ in ‘struct Eigen::internal::traits<Eigen::RefBase<Eigen::Ref<MyVectorType> > >’
EIGEN_DENSE_PUBLIC_INTERFACE(RefBase)
^
/usr/include/eigen3/Eigen/src/Core/Ref.h:67:3: error: no type named ‘StorageIndex’ in ‘struct Eigen::internal::traits<Eigen::RefBase<Eigen::Ref<MyVectorType> > >’
EIGEN_DENSE_PUBLIC_INTERFACE(RefBase)
^
/usr/include/eigen3/Eigen/src/Core/Ref.h:67:3: error: ‘RowsAtCompileTime’ is not a member of ‘Eigen::internal::traits<Eigen::RefBase<Eigen::Ref<MyVectorType> > >’
EIGEN_DENSE_PUBLIC_INTERFACE(RefBase)
^
/usr/include/eigen3/Eigen/src/Core/Ref.h:67:3: error: ‘ColsAtCompileTime’ is not a member of ‘Eigen::internal::traits<Eigen::RefBase<Eigen::Ref<MyVectorType> > >’
/usr/include/eigen3/Eigen/src/Core/Ref.h:67:3: error: ‘MaxSizeAtCompileTime’ is not a member of ‘Eigen::RefBase<Eigen::Ref<MyVectorType> >::Base {aka Eigen::MapBase<Eigen::Ref<MyVectorType>, 0>}’
/usr/include/eigen3/Eigen/src/Core/Ref.h:67:3: error: ‘IsVectorAtCompileTime’ is not a member of ‘Eigen::RefBase<Eigen::Ref<MyVectorType> >::Base {aka Eigen::MapBase<Eigen::Ref<MyVectorType>, 0>}’
/usr/include/eigen3/Eigen/src/Core/Ref.h:67:3: error: no members matching ‘Eigen::RefBase<Eigen::Ref<MyVectorType> >::Base {aka Eigen::MapBase<Eigen::Ref<MyVectorType>, 0>}::derived’ in ‘Eigen::RefBase<Eigen::Ref<MyVectorType> >::Base {aka class Eigen::MapBase<Eigen::Ref<MyVectorType>, 0>}’
EIGEN_DENSE_PUBLIC_INTERFACE(RefBase)
^
/usr/include/eigen3/Eigen/src/Core/Ref.h:67:3: error: no members matching ‘Eigen::RefBase<Eigen::Ref<MyVectorType> >::Base {aka Eigen::MapBase<Eigen::Ref<MyVectorType>, 0>}::const_cast_derived’ in ‘Eigen::RefBase<Eigen::Ref<MyVectorType> >::Base {aka class Eigen::MapBase<Eigen::Ref<MyVectorType>, 0>}’
EIGEN_DENSE_PUBLIC_INTERFACE(RefBase)
^
In file included from /usr/include/eigen3/Eigen/Core:72:0,
from /usr/include/eigen3/Eigen/Dense:1,
from ../main.cpp:2:
/usr/include/eigen3/Eigen/src/Core/Ref.h: In instantiation of ‘class Eigen::Ref<MyVectorType>’:
../main.cpp:26:10: required from here
/usr/include/eigen3/Eigen/src/Core/Ref.h:201:5: error: no type named ‘Scalar’ in ‘struct Eigen::internal::traits<Eigen::Ref<MyVectorType> >’
EIGEN_DENSE_PUBLIC_INTERFACE(Ref)
^
/usr/include/eigen3/Eigen/src/Core/Ref.h:201:5: error: no type named ‘Scalar’ in ‘struct Eigen::internal::traits<Eigen::Ref<MyVectorType> >’
EIGEN_DENSE_PUBLIC_INTERFACE(Ref)
^
In file included from /usr/include/eigen3/Eigen/Core:72:0,
from /usr/include/eigen3/Eigen/Dense:1,
from ../main.cpp:2:
/usr/include/eigen3/Eigen/src/Core/Ref.h:201:5: error: no type named ‘StorageKind’ in ‘struct Eigen::internal::traits<Eigen::Ref<MyVectorType> >’
EIGEN_DENSE_PUBLIC_INTERFACE(Ref)
^
/usr/include/eigen3/Eigen/src/Core/Ref.h:201:5: error: no type named ‘StorageIndex’ in ‘struct Eigen::internal::traits<Eigen::Ref<MyVectorType> >’
EIGEN_DENSE_PUBLIC_INTERFACE(Ref)
^
/usr/include/eigen3/Eigen/src/Core/Ref.h:201:5: error: ‘RowsAtCompileTime’ is not a member of ‘Eigen::internal::traits<Eigen::Ref<MyVectorType> >’
EIGEN_DENSE_PUBLIC_INTERFACE(Ref)
^
/usr/include/eigen3/Eigen/src/Core/Ref.h:201:5: error: ‘ColsAtCompileTime’ is not a member of ‘Eigen::internal::traits<Eigen::Ref<MyVectorType> >’
/usr/include/eigen3/Eigen/src/Core/Ref.h:201:5: error: no members matching ‘Eigen::Ref<MyVectorType>::Base {aka Eigen::RefBase<Eigen::Ref<MyVectorType> >}::derived’ in ‘Eigen::Ref<MyVectorType>::Base {aka class Eigen::RefBase<Eigen::Ref<MyVectorType> >}’
EIGEN_DENSE_PUBLIC_INTERFACE(Ref)
^
/usr/include/eigen3/Eigen/src/Core/Ref.h:201:5: error: no members matching ‘Eigen::Ref<MyVectorType>::Base {aka Eigen::RefBase<Eigen::Ref<MyVectorType> >}::const_cast_derived’ in ‘Eigen::Ref<MyVectorType>::Base {aka class Eigen::RefBase<Eigen::Ref<MyVectorType> >}’
EIGEN_DENSE_PUBLIC_INTERFACE(Ref)
^
../main.cpp: In function ‘void init(Eigen::Ref<MyVectorType>&)’:
../main.cpp:26:10: error: no match for call to ‘(Eigen::Ref<MyVectorType>) (int&, int&)’
m(i, j) = 1;
^
In file included from /usr/include/eigen3/Eigen/Core:450:0,
from /usr/include/eigen3/Eigen/Dense:1,
from ../main.cpp:2:
/usr/include/eigen3/Eigen/src/Core/Ref.h: In instantiation of ‘struct Eigen::internal::traits<Eigen::Ref<MyVectorType> >::match<Eigen::Matrix<double, -1, -1, 0, 12, 12> >’:
/usr/include/eigen3/Eigen/src/Core/Ref.h:197:63: required by substitution of ‘template<class Derived> Eigen::Ref<MyVectorType>::Ref(const Eigen::PlainObjectBase<Derived>&, typename Eigen::internal::enable_if<(bool)(Eigen::internal::traits<Eigen::Ref<MyVectorType> >::match<Derived>::MatchAtCompileTime), Derived>::type*) [with Derived = Eigen::Matrix<double, -1, -1, 0, 12, 12>]’
../main.cpp:32:32: required from here
/usr/include/eigen3/Eigen/src/Core/Ref.h:44:25: error: incomplete type ‘Eigen::internal::traits<MyVectorType>’ used in nested name specifier
AlignmentMatch = (int(traits<PlainObjectType>::Alignment)==int(Unaligned)) || (DerivedAlignment >= int(Alignment)), // FIXME the first condition is not very clear, it should be replaced by the required alignment
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
In file included from /usr/include/eigen3/Eigen/Core:347:0,
from /usr/include/eigen3/Eigen/Dense:1,
from ../main.cpp:2:
/usr/include/eigen3/Eigen/src/Core/PlainObjectBase.h: In instantiation of ‘static void Eigen::PlainObjectBase<Derived>::_check_template_params() [with Derived = Eigen::Matrix<double, 3, 1, 0, 12, 12>]’:
/usr/include/eigen3/Eigen/src/Core/Matrix.h:261:35: required from ‘Eigen::Matrix<_Scalar, _Rows, _Cols, _Options, _MaxRows, _MaxCols>::Matrix() [with _Scalar = double; int _Rows = 3; int _Cols = 1; int _Options = 0; int _MaxRows = 12; int _MaxCols = 12]’
../main.cpp:7:58: required from here
/usr/include/eigen3/Eigen/src/Core/PlainObjectBase.h:899:7: error: ‘INVALID_MATRIX_TEMPLATE_PARAMETERS’ is not a member of ‘Eigen::internal::static_assertion<false>’
EIGEN_STATIC_ASSERT((EIGEN_IMPLIES(MaxRowsAtCompileTime==1 && MaxColsAtCompileTime!=1, (Options&RowMajor)==RowMajor)
^
In file included from /usr/include/eigen3/Eigen/Core:450:0,
from /usr/include/eigen3/Eigen/Dense:1,
from ../main.cpp:2:
/usr/include/eigen3/Eigen/src/Core/Ref.h: In instantiation of ‘Eigen::RefBase<Derived>::RefBase() [with Derived = Eigen::Ref<MyVectorType>]’:
/usr/include/eigen3/Eigen/src/Core/Ref.h:208:5: required from ‘Eigen::Ref<PlainObjectType, Options, StrideType>::Ref(Eigen::PlainObjectBase<OtherDerived>&, typename Eigen::internal::enable_if<(bool)(typename Eigen::internal::traits<Eigen::Ref<PlainObjectType, RefOptions, StrideType> >::match<Derived>::MatchAtCompileTime), Derived>::type*) [with Derived = Eigen::Matrix<double, -1, -1, 0, 12, 12>; PlainObjectType = MyVectorType; int Options = 0; StrideType = Eigen::OuterStride<>; typename Eigen::internal::enable_if<(bool)(typename Eigen::internal::traits<Eigen::Ref<PlainObjectType, RefOptions, StrideType> >::match<Derived>::MatchAtCompileTime), Derived>::type = Eigen::Matrix<double, -1, -1, 0, 12, 12>]’
../main.cpp:32:32: required from here
/usr/include/eigen3/Eigen/src/Core/Ref.h:86:100: error: no matching function for call to ‘Eigen::MapBase<Eigen::Ref<MyVectorType>, 0>::MapBase(int, int, int)’
StrideType::InnerStrideAtCompileTime==Dynamic?0:StrideType::InnerStrideAtCompileTime)
^
In file included from /usr/include/eigen3/Eigen/Core:448:0,
from /usr/include/eigen3/Eigen/Dense:1,
from ../main.cpp:2:
/usr/include/eigen3/Eigen/src/Core/MapBase.h:37:34: note: candidate: Eigen::MapBase<Eigen::Ref<MyVectorType>, 0>::MapBase()
template<typename Derived> class MapBase<Derived, ReadOnlyAccessors>
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/usr/include/eigen3/Eigen/src/Core/MapBase.h:37:34: note: candidate expects 0 arguments, 3 provided
/usr/include/eigen3/Eigen/src/Core/MapBase.h:37:34: note: candidate: Eigen::MapBase<Eigen::Ref<MyVectorType>, 0>::MapBase(const Eigen::MapBase<Eigen::Ref<MyVectorType>, 0>&)
/usr/include/eigen3/Eigen/src/Core/MapBase.h:37:34: note: candidate expects 1 argument, 3 provided
In file included from /usr/include/eigen3/Eigen/Core:450:0,
from /usr/include/eigen3/Eigen/Dense:1,
from ../main.cpp:2:
/usr/include/eigen3/Eigen/src/Core/Ref.h: In instantiation of ‘void Eigen::RefBase<Derived>::construct(Expression&) [with Expression = Eigen::Matrix<double, -1, -1, 0, 12, 12>; Derived = Eigen::Ref<MyVectorType>]’:
/usr/include/eigen3/Eigen/src/Core/Ref.h:210:22: required from ‘Eigen::Ref<PlainObjectType, Options, StrideType>::Ref(Eigen::PlainObjectBase<OtherDerived>&, typename Eigen::internal::enable_if<(bool)(typename Eigen::internal::traits<Eigen::Ref<PlainObjectType, RefOptions, StrideType> >::match<Derived>::MatchAtCompileTime), Derived>::type*) [with Derived = Eigen::Matrix<double, -1, -1, 0, 12, 12>; PlainObjectType = MyVectorType; int Options = 0; StrideType = Eigen::OuterStride<>; typename Eigen::internal::enable_if<(bool)(typename Eigen::internal::traits<Eigen::Ref<PlainObjectType, RefOptions, StrideType> >::match<Derived>::MatchAtCompileTime), Derived>::type = Eigen::Matrix<double, -1, -1, 0, 12, 12>]’
../main.cpp:32:32: required from here
/usr/include/eigen3/Eigen/src/Core/Ref.h:101:7: error: no matching function for call to ‘Eigen::MapBase<Eigen::Ref<MyVectorType>, 0>::MapBase(Eigen::PlainObjectBase<Eigen::Matrix<double, -1, -1, 0, 12, 12> >::Scalar*, int, Eigen::EigenBase<Eigen::Matrix<double, -1, -1, 0, 12, 12> >::Index)’
::new (static_cast<Base*>(this)) Base(expr.data(), 1, expr.size());
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
In file included from /usr/include/eigen3/Eigen/Core:448:0,
from /usr/include/eigen3/Eigen/Dense:1,
from ../main.cpp:2:
/usr/include/eigen3/Eigen/src/Core/MapBase.h:37:34: note: candidate: Eigen::MapBase<Eigen::Ref<MyVectorType>, 0>::MapBase()
template<typename Derived> class MapBase<Derived, ReadOnlyAccessors>
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/usr/include/eigen3/Eigen/src/Core/MapBase.h:37:34: note: candidate expects 0 arguments, 3 provided
/usr/include/eigen3/Eigen/src/Core/MapBase.h:37:34: note: candidate: Eigen::MapBase<Eigen::Ref<MyVectorType>, 0>::MapBase(const Eigen::MapBase<Eigen::Ref<MyVectorType>, 0>&)
/usr/include/eigen3/Eigen/src/Core/MapBase.h:37:34: note: candidate expects 1 argument, 3 provided
In file included from /usr/include/eigen3/Eigen/Core:450:0,
from /usr/include/eigen3/Eigen/Dense:1,
from ../main.cpp:2:
/usr/include/eigen3/Eigen/src/Core/Ref.h:106:7: error: no matching function for call to ‘Eigen::MapBase<Eigen::Ref<MyVectorType>, 0>::MapBase(Eigen::PlainObjectBase<Eigen::Matrix<double, -1, -1, 0, 12, 12> >::Scalar*, Eigen::EigenBase<Eigen::Matrix<double, -1, -1, 0, 12, 12> >::Index, int)’
::new (static_cast<Base*>(this)) Base(expr.data(), expr.size(), 1);
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
In file included from /usr/include/eigen3/Eigen/Core:448:0,
from /usr/include/eigen3/Eigen/Dense:1,
from ../main.cpp:2:
/usr/include/eigen3/Eigen/src/Core/MapBase.h:37:34: note: candidate: Eigen::MapBase<Eigen::Ref<MyVectorType>, 0>::MapBase()
template<typename Derived> class MapBase<Derived, ReadOnlyAccessors>
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/usr/include/eigen3/Eigen/src/Core/MapBase.h:37:34: note: candidate expects 0 arguments, 3 provided
/usr/include/eigen3/Eigen/src/Core/MapBase.h:37:34: note: candidate: Eigen::MapBase<Eigen::Ref<MyVectorType>, 0>::MapBase(const Eigen::MapBase<Eigen::Ref<MyVectorType>, 0>&)
/usr/include/eigen3/Eigen/src/Core/MapBase.h:37:34: note: candidate expects 1 argument, 3 provided
In file included from /usr/include/eigen3/Eigen/Core:450:0,
from /usr/include/eigen3/Eigen/Dense:1,
from ../main.cpp:2:
/usr/include/eigen3/Eigen/src/Core/Ref.h:109:7: error: no matching function for call to ‘Eigen::MapBase<Eigen::Ref<MyVectorType>, 0>::MapBase(Eigen::PlainObjectBase<Eigen::Matrix<double, -1, -1, 0, 12, 12> >::Scalar*, Eigen::Index, Eigen::Index)’
::new (static_cast<Base*>(this)) Base(expr.data(), expr.rows(), expr.cols());
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
In file included from /usr/include/eigen3/Eigen/Core:448:0,
from /usr/include/eigen3/Eigen/Dense:1,
from ../main.cpp:2:
/usr/include/eigen3/Eigen/src/Core/MapBase.h:37:34: note: candidate: Eigen::MapBase<Eigen::Ref<MyVectorType>, 0>::MapBase()
template<typename Derived> class MapBase<Derived, ReadOnlyAccessors>
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/usr/include/eigen3/Eigen/src/Core/MapBase.h:37:34: note: candidate expects 0 arguments, 3 provided
/usr/include/eigen3/Eigen/src/Core/MapBase.h:37:34: note: candidate: Eigen::MapBase<Eigen::Ref<MyVectorType>, 0>::MapBase(const Eigen::MapBase<Eigen::Ref<MyVectorType>, 0>&)
/usr/include/eigen3/Eigen/src/Core/MapBase.h:37:34: note: candidate expects 1 argument, 3 provided
Is possible to pass by reference a dynamic matrix to a fixed one using a custom child class?
Eigen::Ref is not intended to work with own custom types (that would be possible, but is way to complicated for this purpose). Simply use Eigen::Ref<MyVectorType::Base>. Also if a Ref is intended to be an L-value (i.e., writable), you should pass it by-value, like so
void init(Eigen::Ref<MyVectorType::Base> m) {
// ...
That way you don't need to manually generate a temporary when calling init:
int main() {
Eigen::Matrix<double, Eigen::Dynamic, Eigen::Dynamic, Eigen::AutoAlign, 12,
12> mm(3, 1);
// Eigen::Ref<MyVectorType> rr(mm); // not necessary!
init(mm); // implicitly generates a temporary `Ref` object
// ...
For more details, see the online documentation of Ref: https://eigen.tuxfamily.org/dox/classEigen_1_1Ref.html

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

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;