How to add an eigen SparseMatrix with an eigen dense Matrix? - eigen

The Eigen documentation indicates that support for common Sparse + Dense operations is provided, but I couldn't find any detailed examples.
For example:
Eigen::SparseMatrix<int> x(10, 10);
Eigen::Matrix<int, 10, 10> y;
Eigen::Matrix<int, 10, 10> z = x + y;
gives me the following error that I can't work my way around:
test_dense_sparse_sum.cpp: In function ‘int main()’:
test_dense_sparse_sum.cpp:33:38: error: no match for ‘operator+’ in ‘x + y’
test_dense_sparse_sum.cpp:33:38: note: candidates are:
/home/paulb/mylibs/eigen-3.2.0/Eigen/src/SparseCore/../plugins/CommonCwiseBinaryOps.h:27:1: note: template<class OtherDerived> const Eigen::CwiseBinaryOp<Eigen::internal::scalar_sum_op<typename Eigen::internal::traits<T>::Scalar>, const Derived, const OtherDerived> Eigen::SparseMatrixBase::operator+(const Eigen::SparseMatrixBase<OtherDerived>&) const [with OtherDerived = OtherDerived, Derived = Eigen::SparseMatrix<int, 0, int>, typename Eigen::internal::traits<T>::Scalar = int]
/usr/lib/gcc/x86_64-redhat-linux/4.6.3/../../../../include/c++/4.6.3/bits/stl_iterator.h:327:5: note: template<class _Iterator> std::reverse_iterator<_Iterator> std::operator+(typename std::reverse_iterator<_Iterator>::difference_type, const std::reverse_iterator<_Iterator>&)
/usr/lib/gcc/x86_64-redhat-linux/4.6.3/../../../../include/c++/4.6.3/bits/basic_string.h:2306:5: note: template<class _CharT, class _Traits, class _Alloc> std::basic_string<_CharT, _Traits, _Alloc> std::operator+(const std::basic_string<_CharT, _Traits, _Alloc>&, const std::basic_string<_CharT, _Traits, _Alloc>&)
/usr/lib/gcc/x86_64-redhat-linux/4.6.3/../../../../include/c++/4.6.3/bits/basic_string.tcc:694:5: note: template<class _CharT, class _Traits, class _Alloc> std::basic_string<_CharT, _Traits, _Alloc> std::operator+(const _CharT*, const std::basic_string<_CharT, _Traits, _Alloc>&)
/usr/lib/gcc/x86_64-redhat-linux/4.6.3/../../../../include/c++/4.6.3/bits/basic_string.tcc:710:5: note: template<class _CharT, class _Traits, class _Alloc> std::basic_string<_CharT, _Traits, _Alloc> std::operator+(_CharT, const std::basic_string<_CharT, _Traits, _Alloc>&)
/usr/lib/gcc/x86_64-redhat-linux/4.6.3/../../../../include/c++/4.6.3/bits/basic_string.h:2343:5: note: template<class _CharT, class _Traits, class _Alloc> std::basic_string<_CharT, _Traits, _Alloc> std::operator+(const std::basic_string<_CharT, _Traits, _Alloc>&, const _CharT*)
/usr/lib/gcc/x86_64-redhat-linux/4.6.3/../../../../include/c++/4.6.3/bits/basic_string.h:2359:5: note: template<class _CharT, class _Traits, class _Alloc> std::basic_string<_CharT, _Traits, _Alloc> std::operator+(const std::basic_string<_CharT, _Traits, _Alloc>&, _CharT)
/usr/lib/gcc/x86_64-redhat-linux/4.6.3/../../../../include/c++/4.6.3/complex:321:5: note: template<class _Tp> std::complex<_Tp> std::operator+(const std::complex<_Tp>&, const std::complex<_Tp>&)
/usr/lib/gcc/x86_64-redhat-linux/4.6.3/../../../../include/c++/4.6.3/complex:330:5: note: template<class _Tp> std::complex<_Tp> std::operator+(const std::complex<_Tp>&, const _Tp&)
/usr/lib/gcc/x86_64-redhat-linux/4.6.3/../../../../include/c++/4.6.3/complex:339:5: note: template<class _Tp> std::complex<_Tp> std::operator+(const _Tp&, const std::complex<_Tp>&)
/usr/lib/gcc/x86_64-redhat-linux/4.6.3/../../../../include/c++/4.6.3/complex:440:5: note: template<class _Tp> std::complex<_Tp> std::operator+(const std::complex<_Tp>&)
/usr/lib/gcc/x86_64-redhat-linux/4.6.3/../../../../include/c++/4.6.3/bits/stl_bvector.h:266:3: note: std::_Bit_iterator std::operator+(std::ptrdiff_t, const std::_Bit_iterator&)
/usr/lib/gcc/x86_64-redhat-linux/4.6.3/../../../../include/c++/4.6.3/bits/stl_bvector.h:266:3: note: no known conversion for argument 1 from ‘Eigen::SparseMatrix<int, 0, int>’ to ‘std::ptrdiff_t {aka long int}’
/usr/lib/gcc/x86_64-redhat-linux/4.6.3/../../../../include/c++/4.6.3/bits/stl_bvector.h:352:3: note: std::_Bit_const_iterator std::operator+(std::ptrdiff_t, const std::_Bit_const_iterator&)
/usr/lib/gcc/x86_64-redhat-linux/4.6.3/../../../../include/c++/4.6.3/bits/stl_bvector.h:352:3: note: no known conversion for argument 1 from ‘Eigen::SparseMatrix<int, 0, int>’ to ‘std::ptrdiff_t {aka long int}’

Try this!
Eigen::SparseMatrix<int> x(10, 10);
Eigen::Matrix<int, 10, 10> y;
Eigen::Matrix<int, 10, 10> z = y;
x.addTo(z); // z = x + y

Related

unordered_map insert not using move operation

I am trying to insert element to unordered_map, which should use move operations, but its not using.
For example:
#include <unordered_map>
#include <memory>
#include <utility>
struct S {
int i;
};
int main(){
std::unordered_map<int, std::unique_ptr<S>> map {};
std::unique_ptr<S> s {new S{1}};
map.insert({1, std::move(s)});
return 0;
}
Compiling it as: g++-6 -std=c++11 -c Ex2.cpp
Getting error:
In file included from /usr/include/x86_64-linux-gnu/c++/6/bits/c++allocator.h:33:0,
from /usr/include/c++/6/bits/allocator.h:46,
from /usr/include/c++/6/string:41,
from /usr/include/c++/6/stdexcept:39,
from /usr/include/c++/6/array:39,
from /usr/include/c++/6/tuple:39,
from /usr/include/c++/6/unordered_map:41,
from Ex2.cpp:1:
/usr/include/c++/6/ext/new_allocator.h: In instantiation of ‘void __gnu_cxx::new_allocator<_Tp>::construct(_Up*, _Args&& ...) [with _Up = std::pair<const int, std::unique_ptr<S> >; _Args = {const std::pair<const int, std::unique_ptr<S, std::default_delete<S> > >&}; _Tp = std::pair<const int, std::unique_ptr<S> >]’:
/usr/include/c++/6/bits/alloc_traits.h:455:4: required from ‘static void std::allocator_traits<std::allocator<_CharT> >::construct(std::allocator_traits<std::allocator<_CharT> >::allocator_type&, _Up*, _Args&& ...) [with _Up = std::pair<const int, std::unique_ptr<S> >; _Args = {const std::pair<const int, std::unique_ptr<S, std::default_delete<S> > >&}; _Tp = std::pair<const int, std::unique_ptr<S> >; std::allocator_traits<std::allocator<_CharT> >::allocator_type = std::allocator<std::pair<const int, std::unique_ptr<S> > >]’
/usr/include/c++/6/bits/hashtable_policy.h:1953:37: required from ‘std::__detail::_Hashtable_alloc<_NodeAlloc>::__node_type* std::__detail::_Hashtable_alloc<_NodeAlloc>::_M_allocate_node(_Args&& ...) [with _Args = {const std::pair<const int, std::unique_ptr<S, std::default_delete<S> > >&}; _NodeAlloc = std::allocator<std::__detail::_Hash_node<std::pair<const int, std::unique_ptr<S> >, false> >; std::__detail::_Hashtable_alloc<_NodeAlloc>::__node_type = std::__detail::_Hash_node<std::pair<const int, std::unique_ptr<S> >, false>]’
/usr/include/c++/6/bits/hashtable_policy.h:180:58: required from ‘std::__detail::_AllocNode<_NodeAlloc>::__node_type* std::__detail::_AllocNode<_NodeAlloc>::operator()(_Arg&&) const [with _Arg = const std::pair<const int, std::unique_ptr<S> >&; _NodeAlloc = std::allocator<std::__detail::_Hash_node<std::pair<const int, std::unique_ptr<S> >, false> >; std::__detail::_AllocNode<_NodeAlloc>::__node_type = std::__detail::_Hash_node<std::pair<const int, std::unique_ptr<S> >, false>]’
/usr/include/c++/6/bits/hashtable.h:1690:18: required from ‘std::pair<typename std::__detail::_Hashtable_base<_Key, _Value, _ExtractKey, _Equal, _H1, _H2, _Hash, _Traits>::iterator, bool> std::_Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal, _H1, _H2, _Hash, _RehashPolicy, _Traits>::_M_insert(_Arg&&, const _NodeGenerator&, std::true_type) [with _Arg = const std::pair<const int, std::unique_ptr<S> >&; _NodeGenerator = std::__detail::_AllocNode<std::allocator<std::__detail::_Hash_node<std::pair<const int, std::unique_ptr<S> >, false> > >; _Key = int; _Value = std::pair<const int, std::unique_ptr<S> >; _Alloc = std::allocator<std::pair<const int, std::unique_ptr<S> > >; _ExtractKey = std::__detail::_Select1st; _Equal = std::equal_to<int>; _H1 = std::hash<int>; _H2 = std::__detail::_Mod_range_hashing; _Hash = std::__detail::_Default_ranged_hash; _RehashPolicy = std::__detail::_Prime_rehash_policy; _Traits = std::__detail::_Hashtable_traits<false, false, true>; typename std::__detail::_Hashtable_base<_Key, _Value, _ExtractKey, _Equal, _H1, _H2, _Hash, _Traits>::iterator = std::__detail::_Node_iterator<std::pair<const int, std::unique_ptr<S> >, false, false>; std::true_type = std::integral_constant<bool, true>]’
/usr/include/c++/6/bits/hashtable_policy.h:713:55: required from ‘std::__detail::_Insert_base<_Key, _Value, _Alloc, _ExtractKey, _Equal, _H1, _H2, _Hash, _RehashPolicy, _Traits>::__ireturn_type std::__detail::_Insert_base<_Key, _Value, _Alloc, _ExtractKey, _Equal, _H1, _H2, _Hash, _RehashPolicy, _Traits>::insert(const value_type&) [with _Key = int; _Value = std::pair<const int, std::unique_ptr<S> >; _Alloc = std::allocator<std::pair<const int, std::unique_ptr<S> > >; _ExtractKey = std::__detail::_Select1st; _Equal = std::equal_to<int>; _H1 = std::hash<int>; _H2 = std::__detail::_Mod_range_hashing; _Hash = std::__detail::_Default_ranged_hash; _RehashPolicy = std::__detail::_Prime_rehash_policy; _Traits = std::__detail::_Hashtable_traits<false, false, true>; std::__detail::_Insert_base<_Key, _Value, _Alloc, _ExtractKey, _Equal, _H1, _H2, _Hash, _RehashPolicy, _Traits>::__ireturn_type = std::pair<std::__detail::_Node_iterator<std::pair<const int, std::unique_ptr<S> >, false, false>, bool>; std::__detail::_Insert_base<_Key, _Value, _Alloc, _ExtractKey, _Equal, _H1, _H2, _Hash, _RehashPolicy, _Traits>::value_type = std::pair<const int, std::unique_ptr<S> >]’
/usr/include/c++/6/bits/unordered_map.h:550:31: required from ‘std::pair<typename std::_Hashtable<_Key, std::pair<const _Key, _Tp>, _Alloc, std::__detail::_Select1st, _Pred, _Hash, std::__detail::_Mod_range_hashing, std::__detail::_Default_ranged_hash, std::__detail::_Prime_rehash_policy, std::__detail::_Hashtable_traits<std::__not_<std::__and_<std::__is_fast_hash<_Hash>, std::__detail::__is_noexcept_hash<_Key, _Hash> > >::value, false, true> >::iterator, bool> std::unordered_map<_Key, _Tp, _Hash, _Pred, _Alloc>::insert(const value_type&) [with _Key = int; _Tp = std::unique_ptr<S>; _Hash = std::hash<int>; _Pred = std::equal_to<int>; _Alloc = std::allocator<std::pair<const int, std::unique_ptr<S> > >; typename std::_Hashtable<_Key, std::pair<const _Key, _Tp>, _Alloc, std::__detail::_Select1st, _Pred, _Hash, std::__detail::_Mod_range_hashing, std::__detail::_Default_ranged_hash, std::__detail::_Prime_rehash_policy, std::__detail::_Hashtable_traits<std::__not_<std::__and_<std::__is_fast_hash<_Hash>, std::__detail::__is_noexcept_hash<_Key, _Hash> > >::value, false, true> >::iterator = std::__detail::_Node_iterator<std::pair<const int, std::unique_ptr<S> >, false, false>; std::unordered_map<_Key, _Tp, _Hash, _Pred, _Alloc>::value_type = std::pair<const int, std::unique_ptr<S> >]’
Ex2.cpp:14:30: required from here
/usr/include/c++/6/ext/new_allocator.h:120:4: error: use of deleted function ‘constexpr std::pair<_T1, _T2>::pair(const std::pair<_T1, _T2>&) [with _T1 = const int; _T2 = std::unique_ptr<S>]’
{ ::new((void *)__p) _Up(std::forward<_Args>(__args)...); }
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
In file included from /usr/include/c++/6/utility:70:0,
from /usr/include/c++/6/unordered_map:38,
from Ex2.cpp:1:
/usr/include/c++/6/bits/stl_pair.h:224:17: note: ‘constexpr std::pair<_T1, _T2>::pair(const std::pair<_T1, _T2>&) [with _T1 = const int; _T2 = std::unique_ptr<S>]’ is implicitly deleted because the default definition would be ill-formed:
constexpr pair(const pair&) = default;
^~~~
/usr/include/c++/6/bits/stl_pair.h:224:17: error: use of deleted function ‘std::unique_ptr<_Tp, _Dp>::unique_ptr(const std::unique_ptr<_Tp, _Dp>&) [with _Tp = S; _Dp = std::default_delete<S>]’
In file included from /usr/include/c++/6/memory:81:0,
from Ex2.cpp:2:
/usr/include/c++/6/bits/unique_ptr.h:356:7: note: declared here
unique_ptr(const unique_ptr&) = delete;
^~~~~~~~~~
So, error seems telling me that I am using deleted copy constructor of std::pair, which is deleted because std::unique_ptr doesn't have copy constructor. BUT why its not using move constructor?
It compiles fine in my visual studio 2017.
I am using g++ compiler version: gcc version 6.2.0 20160901

C++: Error retrieving STL map from another map

I have the following piece of code -
#include <map>
using Type1 = std::map<std::string, unsigned long long>;
using Type2 = std::map<std::string, Type1>;
class T
{
private:
Type2 mymap;
public:
const Type1& get(const std::string& key) const;
};
const Type1& T::get(const std::string& key) const
{
return mymap[key];
}
int main(void)
{
}
This does not compile and the compiler complains -
maps.cpp: In member function ‘const Type1& T::get(const string&)
const’: maps.cpp:17:20: error: passing ‘const Type2 {aka const
std::map<std::basic_string<char>, std::map<std::basic_string<char>,
long long unsigned int> >}’ as ‘this’ argument of ‘std::map<_Key, _Tp,
_Compare, _Alloc>::mapped_type& std::map<_Key, _Tp, _Compare, _Alloc>::operator[](const key_type&) [with _Key = std::basic_string<char>; _Tp = std::map<std::basic_string<char>, long
long unsigned int>; _Compare = std::less<std::basic_string<char> >;
_Alloc = std::allocator<std::pair<const std::basic_string<char>, std::map<std::basic_string<char>, long long unsigned int> > >;
std::map<_Key, _Tp, _Compare, _Alloc>::mapped_type =
std::map<std::basic_string<char>, long long unsigned int>;
std::map<_Key, _Tp, _Compare, _Alloc>::key_type =
std::basic_string<char>]’ discards qualifiers [-fpermissive]
return mymap[key];
^
I need help understanding this error. From what I can tell I am not modifying "this" in the get function.
Thanks!
The errors comes from the fact that
std::map::operator[] can
inserts the key if the it does not exist.
You could do a std::map::find first as a check.

Strange gdb c++ error

I am making a commandline program in C++ and have run into some strange problems.
I am very new to C++, so this is probably why I am so baffled.
I am using MinGW to compile and debug.
Here is the current code:
#include <string>
#include <vector>
#include <iostream>
#include <stdlib.h>
using namespace std;
// Global Vars
string headerSpace = "\t\t";
string instructionSpace = "\t";
string prmpt = "WIO:+>";
string daysOFWeek [7] = {"mon", "tue", "wed", "thur", "fri", "sat", "sun"};
vector<string> commandHelpGet ("To view a day's exercises type:: get (date)\n", "To view today's required exercises type:: get required\n");
vector<string> commandHelpRem ( "To clear a day's record type:: remove (date)\n", "To remove an exercise from your routine type:: remove (name)\n");
vector<string> commandHelpRec ("To record a day's exercises type:: record (date)\n");
vector<string> commandHelpAdd ("To add an exercise to your routine type:: add (name) (rep/min) (daysOFWeek)\n");
// Method prototypes
vector<string> splitString(string input, string splitter);
void processGetCom(vector<string> input);
void processRecordCom(vector<string> input);
void processRemoveCom(vector<string> input);
void processAddCom(vector<string> input);
void help(string context);
void printError(string reason);
string requiredExercises();
string getRecord(string input);
int main()
{
cout << "\n\n" + headerSpace + "---------- Welcome to WorkItOut! ----------\n";
help("");
string inRaw = "";
vector<string> input;
while (true)
{
cout << prmpt;
string inRaw = "";
getline(cin, inRaw);
input = splitString(inRaw, " ");
// Overflow guard
if (input.size() > 4)
{
printError("To many arguments; Please enter a valid command");
}
if (input[0].compare("get") == 0)
{
processGetCom(input);
}
else if (input[0].compare("record") == 0)
{
processRecordCom(input);
}
else if (input[0].compare("remove") == 0)
{
processRemoveCom(input);
}
else if (input[0].compare("add") == 0)
{
processAddCom(input);
}
else if (input[0].compare("exit") == 0)
{
exit(0);
}
}
}
/* Updated C. A. Acred on 05/22/2015
* This method takes a string and splits it into a vector with the second input.
*/
vector<string> splitString(string input, string splitter)
{
vector<string> output; // initialize output vector
int currentElement = 0; // set current vector element to 0
string currentChar; // declare a container for the current char
output.push_back(""); // Push an empty string onto output
for(int i = 0; i < input.length(); i++) // for each char in input
{
currentChar = input.substr(i, 1); // currentChar = the char in input # i
if (currentChar.compare(splitter) == 0) // if this char and the splitter are the same
{
while (currentChar.compare(splitter) == 0) // while they are the same
{
i++; // advance i by 1
currentChar = input.substr(i, 1); // get the next char
}
output.push_back(currentChar); // put the current char as the first in a new element
currentElement++; // point to the new element
}
else
{
output[currentElement] = output[currentElement] + currentChar; // output # currentElement += currentChar
}
}
return output;
}
string requiredExercises()
{
return "yo";
}
/* Updated C. A. Acred on 5/27/15
* Processes get commands.
*/
void processGetCom(vector<string> input)
{
if (input.size() > 2)
{
printError("To many arguments; Enter a valid command");
}
if (input[1].compare("required") == 0)
{
cout << requiredExercises();
}
else
{
cout << getRecord(input[1]);
}
}
void processRecordCom(vector<string> input)
{
}
void processRemoveCom(vector<string> input)
{
}
void processAddCom(vector<string> input)
{
}
void printError(string reason)
{
}
/** Updated C. A. Acred 05/22/2015
* Print help on commands.
*/
void help(string context)
{
if (context.empty())
{
int i;
for (i = 0; i < commandHelpGet.size(); i++)
{
cout << commandHelpGet[i];
}
for (i = 0; i < commandHelpRec.size(); i++)
{
cout << commandHelpRec[i];
}
for (i = 0; i < commandHelpRem.size(); i++)
{
cout << commandHelpRem[i];
}
for (i = 0; i < commandHelpAdd.size(); i++)
{
cout << commandHelpAdd[i];
}
}
cout << instructionSpace + "To exit type:: exit\n";
cout << instructionSpace + "-+| date format = m/d/yr |+-\n\t\tExample:: 05/20/2015\n";
cout << instructionSpace + "-+| daysOFWeek format = day/day/day..etc(up to 7) |+-\n\t\tExample:: mon/tue/wed/thur/fri/sat/sun\n\n\n\n";
cout << instructionSpace + "-+| rep format = rep:(#) |+-\n\t\tExample:: rep:15\n\n\n\n";
cout << instructionSpace + "-+| min format = min:(#) |+-\n\t\tExample:: min:15\n\n\n\n";
}
And here is the strange error I am getting from g++:
main.cpp:24:84: error: invalid conversion from 'const char*' to 'unsigned int' [-fpermissive]
c:\mingw\bin\../lib/gcc/mingw32/4.6.2/include/c++/bits/stl_vector.h:263:7: error: initializing argument 1 of 'std::vector<_Tp, _Alloc>::vector(std::vector<_Tp, _Alloc>::size_type, const value_type&, const allocator_type&) [with _Tp = std::basic_string<char>, _Alloc = std::allocator<std::basic_stri
ng<char> >, std::vector<_Tp, _Alloc>::size_type = unsigned int, std::vector<_Tp, _Alloc>::value_type = std::basic_string<char>, std::vector<_Tp, _Alloc>::allocator_type = std::allocator<std::basic_string<char> >]' [-fpermissive]
main.cpp:25:111: error: invalid conversion from 'const char*' to 'unsigned int' [-fpermissive]
c:\mingw\bin\../lib/gcc/mingw32/4.6.2/include/c++/bits/stl_vector.h:263:7: error: initializing argument 1 of 'std::vector<_Tp, _Alloc>::vector(std::vector<_Tp, _Alloc>::size_type, const value_type&, const allocator_type&) [with _Tp = std::basic_string<char>, _Alloc = std::allocator<std::basic_stri
ng<char> >, std::vector<_Tp, _Alloc>::size_type = unsigned int, std::vector<_Tp, _Alloc>::value_type = std::basic_string<char>, std::vector<_Tp, _Alloc>::allocator_type = std::allocator<std::basic_string<char> >]' [-fpermissive]
In file included from c:\mingw\bin\../lib/gcc/mingw32/4.6.2/include/c++/vector:63:0,
from main.cpp:10:
c:\mingw\bin\../lib/gcc/mingw32/4.6.2/include/c++/bits/stl_construct.h: In function 'void std::_Construct(_T1*, const _T2&) [with _T1 = std::basic_string<char>, _T2 = char]':
c:\mingw\bin\../lib/gcc/mingw32/4.6.2/include/c++/bits/stl_uninitialized.h:77:3: instantiated from 'static _ForwardIterator std::__uninitialized_copy<_TrivialValueTypes>::__uninit_copy(_InputIterator, _InputIterator, _ForwardIterator) [with _InputIterator = const char*, _ForwardIterator = std::bas
ic_string<char>*, bool _TrivialValueTypes = false]'
c:\mingw\bin\../lib/gcc/mingw32/4.6.2/include/c++/bits/stl_uninitialized.h:119:41: instantiated from '_ForwardIterator std::uninitialized_copy(_InputIterator, _InputIterator, _ForwardIterator) [with _InputIterator = const char*, _ForwardIterator = std::basic_string<char>*]'
c:\mingw\bin\../lib/gcc/mingw32/4.6.2/include/c++/bits/stl_uninitialized.h:259:63: instantiated from '_ForwardIterator std::__uninitialized_copy_a(_InputIterator, _InputIterator, _ForwardIterator, std::allocator<_Tp>&) [with _InputIterator = const char*, _ForwardIterator = std::basic_string<char>*
, _Tp = std::basic_string<char>]'
c:\mingw\bin\../lib/gcc/mingw32/4.6.2/include/c++/bits/stl_vector.h:1113:4: instantiated from 'void std::vector<_Tp, _Alloc>::_M_range_initialize(_ForwardIterator, _ForwardIterator, std::forward_iterator_tag) [with _ForwardIterator = const char*, _Tp = std::basic_string<char>, _Alloc = std::alloca
tor<std::basic_string<char> >]'
c:\mingw\bin\../lib/gcc/mingw32/4.6.2/include/c++/bits/stl_vector.h:1091:4: instantiated from 'void std::vector<_Tp, _Alloc>::_M_initialize_dispatch(_InputIterator, _InputIterator, std::__false_type) [with _InputIterator = const char*, _Tp = std::basic_string<char>, _Alloc = std::allocator<std::ba
sic_string<char> >]'
c:\mingw\bin\../lib/gcc/mingw32/4.6.2/include/c++/bits/stl_vector.h:340:4: instantiated from 'std::vector<_Tp, _Alloc>::vector(_InputIterator, _InputIterator, const allocator_type&) [with _InputIterator = const char*, _Tp = std::basic_string<char>, _Alloc = std::allocator<std::basic_string<char> >
, std::vector<_Tp, _Alloc>::allocator_type = std::allocator<std::basic_string<char> >]'
main.cpp:22:140: instantiated from here
c:\mingw\bin\../lib/gcc/mingw32/4.6.2/include/c++/bits/stl_construct.h:84:7: error: invalid conversion from 'char' to 'const char*' [-fpermissive]
c:\mingw\bin\../lib/gcc/mingw32/4.6.2/include/c++/bits/basic_string.tcc:214:5: error: initializing argument 1 of 'std::basic_string<_CharT, _Traits, _Alloc>::basic_string(const _CharT*, const _Alloc&) [with _CharT = char, _Traits = std::char_traits<char>, _Alloc = std::allocator<char>]' [-fpermiss
ive]
Any help with understanding and fixing this error is most appreciated.
UPDATE #1:
I changed the initialization of the vectors commandHelpGet - commandHelpAdd to use curly braces, but now I get a new error:
main.cpp:22:16: warning: extended initializer lists only available with -std=c++0x or -std=gnu++0x [enabled by default]
main.cpp:22:139: error: in C++98 'commandHelpGet' must be initialized by constructor, not by '{...}'
main.cpp:23:16: warning: extended initializer lists only available with -std=c++0x or -std=gnu++0x [enabled by default]
main.cpp:23:147: error: in C++98 'commandHelpRem' must be initialized by constructor, not by '{...}'
main.cpp:24:16: warning: extended initializer lists only available with -std=c++0x or -std=gnu++0x [enabled by default]
main.cpp:24:84: error: in C++98 'commandHelpRec' must be initialized by constructor, not by '{...}'
main.cpp:24:84: error: invalid conversion from 'const char*' to 'unsigned int' [-fpermissive]
c:\mingw\bin\../lib/gcc/mingw32/4.6.2/include/c++/bits/stl_vector.h:263:7: error: initializing argument 1 of 'std::vector<_Tp, _Alloc>::vector(std::vector<_Tp, _Alloc>::size_type, const value_type&, const allocator_type&) [with _Tp = std::basic_string<char>, _Alloc = std::allocator<std::basic_stri
ng<char> >, std::vector<_Tp, _Alloc>::size_type = unsigned int, std::vector<_Tp, _Alloc>::value_type = std::basic_string<char>, std::vector<_Tp, _Alloc>::allocator_type = std::allocator<std::basic_string<char> >]' [-fpermissive]
main.cpp:25:16: warning: extended initializer lists only available with -std=c++0x or -std=gnu++0x [enabled by default]
main.cpp:25:111: error: in C++98 'commandHelpAdd' must be initialized by constructor, not by '{...}'
main.cpp:25:111: error: invalid conversion from 'const char*' to 'unsigned int' [-fpermissive]
c:\mingw\bin\../lib/gcc/mingw32/4.6.2/include/c++/bits/stl_vector.h:263:7: error: initializing argument 1 of 'std::vector<_Tp, _Alloc>::vector(std::vector<_Tp, _Alloc>::size_type, const value_type&, const allocator_type&) [with _Tp = std::basic_string<char>, _Alloc = std::allocator<std::basic_stri
ng<char> >, std::vector<_Tp, _Alloc>::size_type = unsigned int, std::vector<_Tp, _Alloc>::value_type = std::basic_string<char>, std::vector<_Tp, _Alloc>::allocator_type = std::allocator<std::basic_string<char> >]' [-fpermissive]
In file included from c:\mingw\bin\../lib/gcc/mingw32/4.6.2/include/c++/vector:63:0,
from main.cpp:10:
c:\mingw\bin\../lib/gcc/mingw32/4.6.2/include/c++/bits/stl_construct.h: In function 'void std::_Construct(_T1*, const _T2&) [with _T1 = std::basic_string<char>, _T2 = char]':
c:\mingw\bin\../lib/gcc/mingw32/4.6.2/include/c++/bits/stl_uninitialized.h:77:3: instantiated from 'static _ForwardIterator std::__uninitialized_copy<_TrivialValueTypes>::__uninit_copy(_InputIterator, _InputIterator, _ForwardIterator) [with _InputIterator = const char*, _ForwardIterator = std::bas
ic_string<char>*, bool _TrivialValueTypes = false]'
c:\mingw\bin\../lib/gcc/mingw32/4.6.2/include/c++/bits/stl_uninitialized.h:119:41: instantiated from '_ForwardIterator std::uninitialized_copy(_InputIterator, _InputIterator, _ForwardIterator) [with _InputIterator = const char*, _ForwardIterator = std::basic_string<char>*]'
c:\mingw\bin\../lib/gcc/mingw32/4.6.2/include/c++/bits/stl_uninitialized.h:259:63: instantiated from '_ForwardIterator std::__uninitialized_copy_a(_InputIterator, _InputIterator, _ForwardIterator, std::allocator<_Tp>&) [with _InputIterator = const char*, _ForwardIterator = std::basic_string<char>*
, _Tp = std::basic_string<char>]'
c:\mingw\bin\../lib/gcc/mingw32/4.6.2/include/c++/bits/stl_vector.h:1113:4: instantiated from 'void std::vector<_Tp, _Alloc>::_M_range_initialize(_ForwardIterator, _ForwardIterator, std::forward_iterator_tag) [with _ForwardIterator = const char*, _Tp = std::basic_string<char>, _Alloc = std::alloca
tor<std::basic_string<char> >]'
c:\mingw\bin\../lib/gcc/mingw32/4.6.2/include/c++/bits/stl_vector.h:1091:4: instantiated from 'void std::vector<_Tp, _Alloc>::_M_initialize_dispatch(_InputIterator, _InputIterator, std::__false_type) [with _InputIterator = const char*, _Tp = std::basic_string<char>, _Alloc = std::allocator<std::ba
sic_string<char> >]'
c:\mingw\bin\../lib/gcc/mingw32/4.6.2/include/c++/bits/stl_vector.h:340:4: instantiated from 'std::vector<_Tp, _Alloc>::vector(_InputIterator, _InputIterator, const allocator_type&) [with _InputIterator = const char*, _Tp = std::basic_string<char>, _Alloc = std::allocator<std::basic_string<char> >
, std::vector<_Tp, _Alloc>::allocator_type = std::allocator<std::basic_string<char> >]'
main.cpp:22:139: instantiated from here
c:\mingw\bin\../lib/gcc/mingw32/4.6.2/include/c++/bits/stl_construct.h:84:7: error: invalid conversion from 'char' to 'const char*' [-fpermissive]
c:\mingw\bin\../lib/gcc/mingw32/4.6.2/include/c++/bits/basic_string.tcc:214:5: error: initializing argument 1 of 'std::basic_string<_CharT, _Traits, _Alloc>::basic_string(const _CharT*, const _Alloc&) [with _CharT = char, _Traits = std::char_traits<char>, _Alloc = std::allocator<char>]' [-fpermiss
ive]
- CAA14
Take a look at your vector initializations. If your intention is to use list syntax, then use braces instead of parens:
vector<string> commandHelpGet { "To view a day's exercises type:: get (date)\n", "To view today's required exercises type:: get required\n" };
Look at the examples here.

C++11 Allocator default interfaces I have to provide

When creating my own allocator in c++11 I am implementing the following interfaces. This works with vector but when trying to use this with map I get errors on missing items. I thought this was all I need to implement for c++11 since it will use allocator_traits in the stl implementations. What am I missing here? Do I need to define more methods / data structures for an allocator for std::map? I am seeing the following errors when trying to compile currently (see below). Line 3 main.cpp is just
#include <map>
template <class T> struct MyAllocator {
typedef T value_type;
MyAllocator() noexcept; // only required if used
MyAllocator(const MyAllocator&) noexcept; // copies must be equal
MyAllocator(MyAllocator&&) noexcept; // not needed if copy ctor is good enough
template <class U> MyAllocator(const MyAllocator<U>& u) noexcept;
// requires: *this == MyAllocator(u)
value_type* allocate(std::size_t);
void deallocate(value_type*, std::size_t) noexcept;
};
template <class T, class U> bool
operator==(const MyAllocator<T>&, const MyAllocator<U>&) noexcept;
Errors:
In file included from
/opt/gcc-4.8.1/usr/local/include/c++/4.8.1/map:61:0,
from main.cpp:3:
/opt/gcc-4.8.1/usr/local/include/c++/4.8.1/bits/stl_map.h: In
instantiation of ‘class std::map,
MyAlloc >’:
main.cpp:146:14: required from here
/opt/gcc-4.8.1/usr/local/include/c++/4.8.1/bits/stl_map.h:143:58:
error: no type named ‘pointer’ in ‘std::map,
MyAlloc >::_Pair_alloc_type {aka class
MyAlloc, 200 typedef typename
_Pair_alloc_type::pointer pointer; ^
/opt/gcc-4.8.1/usr/local/include/c++/4.8.1/bits/stl_map.h:144:58:
error: no type named ‘const_pointer’ in ‘std::map, MyAlloc >::_Pair_alloc_type {aka class
MyAlloc
/opt/gcc-4.8.1/usr/local/include/c++/4.8.1/bits/stl_map.h:145:58:
error: no type named ‘reference’ in ‘std::map, MyAlloc >::_Pair_alloc_type {aka class
MyAlloc, 2 typedef typename
_Pair_alloc_type::reference reference; ^
/opt/gcc-4.8.1/usr/local/include/c++/4.8.1/bits/stl_map.h:146:58:
error: no type named ‘const_reference’ in ‘std::map, MyAlloc >::_Pair_alloc_type {aka class
MyAlloc
In file included from
/opt/gcc-4.8.1/usr/local/include/c++/4.8.1/map:60:0,
from main.cpp:3:
/opt/gcc-4.8.1/usr/local/include/c++/4.8.1/bits/stl_tree.h: In
instantiation of ‘void std::_Rb_tree<_Key, _Val, _KeyOfValue,
_Compare, _Alloc>::_M_destroy_node(std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::_Link_t /opt/gcc-4.8.1/usr/local/include/c++/4.8.1/bits/stl_tree.h:1124:23:
required from ‘void std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare,
_Alloc>::_M_erase(std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::_Link_type /opt/gcc-4.8.1/usr/local/include/c++/4.8.1/bits/stl_tree.h:671:28:
required from ‘std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare,
_Alloc>::~_Rb_tree() [with _Key = int; _Val = std::pair; _KeyOfValue = std::
/opt/gcc-4.8.1/usr/local/include/c++/4.8.1/bits/stl_map.h:96:11:
required from here
/opt/gcc-4.8.1/usr/local/include/c++/4.8.1/bits/stl_tree.h:421:2:
error: ‘std::_Rb_tree,
std::_Select1st >, std::less,
MyAlloc, 200ul> >:
_M_get_Node_allocator().destroy(__p); ^
make: *** [main.o] Error 1
The first errors are solved using some typedefs within MyAllocator:
typedef T& reference;
typedef const T& const_reference;
typedef T* pointer;
typedef const T* const_pointer;
Please post your new compilation output.
Basically as my comment suggested I needed to put the typedefs for the following and a construct and destroy
template <class U>
struct rebind {typedef MyAlloc<U, N> other;};
typedef size_t size_type;
typedef ptrdiff_t difference_type;
typedef T* pointer;
typedef const T* const_pointer;
typedef T& reference;
typedef const T& const_reference;
typedef T value_type;
/// Call constructor with many arguments
template<typename U, typename... Args>
void construct(U* p, Args&&... args)
{
// Placement new
::new((void *)p) U(std::forward<Args>(args)...);
}
/// Call destructor
template<typename U>
void destroy(U* p)
{
p->~U();
}

std::map::insert trouble

I have very simple and stupid trouble:
std::map<b2Vec2, b2Body*> mTakePoints;
mTakePoints.insert(std::make_pair(point, body));
Compiler says:
In file included from /usr/include/c++/4.4/string:50,
from /usr/include/ClanLib-2.2/ClanLib/Display/../Core/Text/string_types.h:34,
from /usr/include/ClanLib-2.2/ClanLib/Display/display.h:35,
from /usr/include/ClanLib-2.2/ClanLib/display.h:40,
from /home/pfight/Themisto/include/World/Actions/Action.hpp:21,
from /home/pfight/Themisto/include/World/Actions/TakeAction.hpp:21,
from /home/pfight/Themisto/src/World/Actions/TakeAction.cpp:18:
/usr/include/c++/4.4/bits/stl_function.h: In member function ‘bool std::less<_Tp>::operator()(const _Tp&, const _Tp&) const [with _Tp = b2Vec2]’:
/usr/include/c++/4.4/bits/stl_tree.h:1170: instantiated from ‘std::pair<typename std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::iterator, bool> std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::_M_insert_unique(const _Val&) [with _Key = b2Vec2, _Val = std::pair<const b2Vec2, b2Body*>, _KeyOfValue = std::_Select1st<std::pair<const b2Vec2, b2Body*> >, _Compare = std::less<b2Vec2>, _Alloc = std::allocator<std::pair<const b2Vec2, b2Body*> >]’
/usr/include/c++/4.4/bits/stl_map.h:500: instantiated from ‘std::pair<typename std::_Rb_tree<_Key, std::pair<const _Key, _Tp>, std::_Select1st<std::pair<const _Key, _Tp> >, _Compare, typename _Alloc::rebind<std::pair<const _Key, _Tp> >::other>::iterator, bool> std::map<_Key, _Tp, _Compare, _Alloc>::insert(const std::pair<const _Key, _Tp>&) [with _Key = b2Vec2, _Tp = b2Body*, _Compare = std::less<b2Vec2>, _Alloc = std::allocator<std::pair<const b2Vec2, b2Body*> >]’
/home/pfight/Themisto/src/World/Actions/TakeAction.cpp:43: instantiated from here
/usr/include/c++/4.4/bits/stl_function.h:230: error: no match for ‘operator<’ in ‘__x < __y’
/usr/include/ClanLib-2.2/ClanLib/Display/../Core/Text/string_data16.h:383: note: candidates are: bool operator<(const CL_StringData16&, const wchar_t*)
/usr/include/ClanLib-2.2/ClanLib/Display/../Core/Text/string_data16.h:382: note: bool operator<(const wchar_t*, const CL_StringData16&)
/usr/include/ClanLib-2.2/ClanLib/Display/../Core/Text/string_data16.h:381: note: bool operator<(const CL_StringData16&, const CL_StringData16&)
/usr/include/ClanLib-2.2/ClanLib/Display/../Core/Text/string_data8.h:383: note: bool operator<(const CL_StringData8&, const char*)
/usr/include/ClanLib-2.2/ClanLib/Display/../Core/Text/string_data8.h:382: note: bool operator<(const char*, const CL_StringData8&)
/usr/include/ClanLib-2.2/ClanLib/Display/../Core/Text/string_data8.h:381: note: bool operator<(const CL_StringData8&, const CL_StringData8&)
TakeAction.cpp:43 it is row with insert call. For fun I tried next:
std::pair<b2Vec2, b2Body*> item(point, body);
mTakePoints.insert(item);
All the same.
I'm pretty confused, explain please, what is wrong?
Did you define the following operator:?
bool operator< (const b2Vec2&, const b2Vec2&)
std::map requires that the less-than operator be defined for keys, or that a less-than operator be provided as a template argument. In particular, std::map is implemented as a (sorted) binary tree, and needs operator< to figure out where to put new items.
For map, set and the like ordered containers must be told where to put new items. This is done by implementing the less-than operator:
bool operator< (const b2Vec2& first, const b2Vec2& second)
{
return first.some_attribute < second.some_attribute;
}
You can see in the compilation error that it was missing:
/usr/include/c++/4.4/bits/stl_function.h:230: error: no match for ‘operator<’ in ‘__x < __y’
The rest of the output tells you that the compiler tried to find a suitable operator and failed.

Resources