Problem description
I have a trivial struct, adapted for boost::fusion:
struct Node
{
std::string text;
};
BOOST_FUSION_ADAPT_STRUCT(Node, (std::string, text))
I have a grammar with a set of rules:
boost::spirit::qi::rule<Iterator, Node()> node_line;
boost::spirit::qi::rule<Iterator, Node()> node;
And there is one rule that blows up in my face:
node_line %= " * " >> node;
I would expect this to be valid, since the attribute of the literal is unused, and node_line and node have the same attribute type. Instead the compiler complains:
/usr/include/boost/spirit/home/support/container.hpp:262:13: error: no matching function for call to ‘std::basic_string<char>::insert(std::basic_string<char>::iterator, const Node&)’
I.e. the compiler tries to append a struct Node onto a std::string. I am utterly confused as to why it would try to do that. Any thoughts?
Full code
#include <boost/config/warning_disable.hpp>
#include <boost/fusion/include/adapt_struct.hpp>
#include <boost/spirit/include/qi.hpp>
struct Node
{
std::string text;
};
BOOST_FUSION_ADAPT_STRUCT(Node, (std::string, text))
template <typename Iterator>
struct My_parser : boost::spirit::qi::grammar<Iterator, Node()>
{
My_parser()
: My_parser::base_type(node_line)
{
name %= +boost::spirit::ascii::char_;
node %= name;
node_line %= " * " >> node;
// node_line %= node;
}
boost::spirit::qi::rule<Iterator, std::string()> name;
boost::spirit::qi::rule<Iterator, Node()> node_line;
boost::spirit::qi::rule<Iterator, Node()> node;
};
int main()
{
const std::string input(" * Some text");
Node parsed;
My_parser<std::string::const_iterator> my_parser;
bool r = parse(input.begin(), input.end(), my_parser, parsed);
if(r)
std::cout << "Match: " << parsed.text << std::endl;
else
std::cout << "No match" << std::endl;
return 0;
}
Full error message
In file included from /usr/include/boost/spirit/home/support/string_traits.hpp:16:0,
from /usr/include/boost/spirit/home/support/nonterminal/expand_arg.hpp:20,
from /usr/include/boost/spirit/home/support/context.hpp:15,
from /usr/include/boost/spirit/home/qi/domain.hpp:18,
from /usr/include/boost/spirit/home/qi/meta_compiler.hpp:15,
from /usr/include/boost/spirit/home/qi/action/action.hpp:14,
from /usr/include/boost/spirit/home/qi/action.hpp:14,
from /usr/include/boost/spirit/home/qi.hpp:14,
from /usr/include/boost/spirit/include/qi.hpp:16,
from tt.cc:3:
/usr/include/boost/spirit/home/support/container.hpp: In static member function ‘static bool boost::spirit::traits::push_back_container<Container, T, Enable>::call(Container&, const T&) [with Container = std::basic_string<char>, T = Node, Enable = void]’:
/usr/include/boost/spirit/home/support/container.hpp:324:62: instantiated from ‘bool boost::spirit::traits::push_back(Container&, const T&) [with Container = std::basic_string<char>, T = Node]’
/usr/include/boost/spirit/home/qi/detail/assign_to.hpp:168:13: instantiated from ‘static void boost::spirit::traits::assign_to_container_from_value<Attribute, T, Enable>::call(const T_&, Attribute&, mpl_::false_, mpl_::false_) [with T_ = Node, Attribute = std::basic_string<char>, T = Node, Enable = void, mpl_::false_ = mpl_::bool_<false>]’
/usr/include/boost/spirit/home/qi/detail/assign_to.hpp:203:13: instantiated from ‘static void boost::spirit::traits::assign_to_container_from_value<Attribute, T, Enable>::call(const T&, Attribute&) [with Attribute = std::basic_string<char>, T = Node, Enable = void]’
/usr/include/boost/spirit/home/qi/detail/assign_to.hpp:243:13: instantiated from ‘void boost::spirit::traits::detail::assign_to(const T&, Attribute&, mpl_::true_, mpl_::true_) [with T = Node, Attribute = std::basic_string<char>, mpl_::true_ = mpl_::bool_<true>]’
/usr/include/boost/spirit/home/qi/detail/assign_to.hpp:257:9: instantiated from ‘void boost::spirit::traits::assign_to(const T&, Attribute&) [with T = Node, Attribute = std::basic_string<char>]’
/usr/include/boost/spirit/home/qi/detail/attributes.hpp:26:13: [ skipping 12 instantiation contexts ]
/usr/include/boost/function/function_template.hpp:913:60: instantiated from ‘void boost::function4<R, T1, T2, T3, T4>::assign_to(Functor) [with Functor = boost::spirit::qi::detail::parser_binder<boost::spirit::qi::sequence<boost::fusion::cons<boost::spirit::qi::literal_string<const char (&)[5], true>, boost::fusion::cons<boost::spirit::qi::reference<const boost::spirit::qi::rule<__gnu_cxx::__normal_iterator<const char*, std::basic_string<char> >, Node(), boost::spirit::unused_type, boost::spirit::unused_type, boost::spirit::unused_type> >, boost::fusion::nil> > >, mpl_::bool_<true> >, R = bool, T0 = __gnu_cxx::__normal_iterator<const char*, std::basic_string<char> >&, T1 = const __gnu_cxx::__normal_iterator<const char*, std::basic_string<char> >&, T2 = boost::spirit::context<boost::fusion::cons<Node&, boost::fusion::nil>, boost::fusion::vector0<> >&, T3 = const boost::spirit::unused_type&]’
/usr/include/boost/function/function_template.hpp:722:7: instantiated from ‘boost::function4<R, T1, T2, T3, T4>::function4(Functor, typename boost::enable_if_c<boost::type_traits::ice_not<boost::is_integral<Functor>::value>::value, int>::type) [with Functor = boost::spirit::qi::detail::parser_binder<boost::spirit::qi::sequence<boost::fusion::cons<boost::spirit::qi::literal_string<const char (&)[5], true>, boost::fusion::cons<boost::spirit::qi::reference<const boost::spirit::qi::rule<__gnu_cxx::__normal_iterator<const char*, std::basic_string<char> >, Node(), boost::spirit::unused_type, boost::spirit::unused_type, boost::spirit::unused_type> >, boost::fusion::nil> > >, mpl_::bool_<true> >, R = bool, T0 = __gnu_cxx::__normal_iterator<const char*, std::basic_string<char> >&, T1 = const __gnu_cxx::__normal_iterator<const char*, std::basic_string<char> >&, T2 = boost::spirit::context<boost::fusion::cons<Node&, boost::fusion::nil>, boost::fusion::vector0<> >&, T3 = const boost::spirit::unused_type&, typename boost::enable_if_c<boost::type_traits::ice_not<boost::is_integral<Functor>::value>::value, int>::type = int]’
/usr/include/boost/function/function_template.hpp:1064:16: instantiated from ‘boost::function<R(T0, T1, T2, T3)>::function(Functor, typename boost::enable_if_c<boost::type_traits::ice_not<boost::is_integral<Functor>::value>::value, int>::type) [with Functor = boost::spirit::qi::detail::parser_binder<boost::spirit::qi::sequence<boost::fusion::cons<boost::spirit::qi::literal_string<const char (&)[5], true>, boost::fusion::cons<boost::spirit::qi::reference<const boost::spirit::qi::rule<__gnu_cxx::__normal_iterator<const char*, std::basic_string<char> >, Node(), boost::spirit::unused_type, boost::spirit::unused_type, boost::spirit::unused_type> >, boost::fusion::nil> > >, mpl_::bool_<true> >, R = bool, T0 = __gnu_cxx::__normal_iterator<const char*, std::basic_string<char> >&, T1 = const __gnu_cxx::__normal_iterator<const char*, std::basic_string<char> >&, T2 = boost::spirit::context<boost::fusion::cons<Node&, boost::fusion::nil>, boost::fusion::vector0<> >&, T3 = const boost::spirit::unused_type&, typename boost::enable_if_c<boost::type_traits::ice_not<boost::is_integral<Functor>::value>::value, int>::type = int]’
/usr/include/boost/function/function_template.hpp:1105:5: instantiated from ‘typename boost::enable_if_c<boost::type_traits::ice_not<boost::is_integral<Functor>::value>::value, boost::function<R(T0, T1, T2, T3)>&>::type boost::function<R(T0, T1, T2, T3)>::operator=(Functor) [with Functor = boost::spirit::qi::detail::parser_binder<boost::spirit::qi::sequence<boost::fusion::cons<boost::spirit::qi::literal_string<const char (&)[5], true>, boost::fusion::cons<boost::spirit::qi::reference<const boost::spirit::qi::rule<__gnu_cxx::__normal_iterator<const char*, std::basic_string<char> >, Node(), boost::spirit::unused_type, boost::spirit::unused_type, boost::spirit::unused_type> >, boost::fusion::nil> > >, mpl_::bool_<true> >, R = bool, T0 = __gnu_cxx::__normal_iterator<const char*, std::basic_string<char> >&, T1 = const __gnu_cxx::__normal_iterator<const char*, std::basic_string<char> >&, T2 = boost::spirit::context<boost::fusion::cons<Node&, boost::fusion::nil>, boost::fusion::vector0<> >&, T3 = const boost::spirit::unused_type&, typename boost::enable_if_c<boost::type_traits::ice_not<boost::is_integral<Functor>::value>::value, boost::function<R(T0, T1, T2, T3)>&>::type = boost::function<bool(__gnu_cxx::__normal_iterator<const char*, std::basic_string<char> >&, const __gnu_cxx::__normal_iterator<const char*, std::basic_string<char> >&, boost::spirit::context<boost::fusion::cons<Node&, boost::fusion::nil>, boost::fusion::vector0<> >&, const boost::spirit::unused_type&)>&]’
/usr/include/boost/spirit/home/qi/nonterminal/rule.hpp:214:13: instantiated from ‘boost::spirit::qi::rule<Iterator, T1, T2, T3, T4>& boost::spirit::qi::operator%=(boost::spirit::qi::rule<Iterator, T1, T2, T3, T4>&, const Expr&) [with Expr = boost::proto::exprns_::expr<boost::proto::tag::shift_right, boost::proto::argsns_::list2<boost::proto::exprns_::expr<boost::proto::tag::terminal, boost::proto::argsns_::term<const char (&)[5]>, 0l>, boost::spirit::qi::rule<__gnu_cxx::__normal_iterator<const char*, std::basic_string<char> >, Node(), boost::spirit::unused_type, boost::spirit::unused_type, boost::spirit::unused_type>&>, 2l>, Iterator = __gnu_cxx::__normal_iterator<const char*, std::basic_string<char> >, T1 = Node(), T2 = boost::spirit::unused_type, T3 = boost::spirit::unused_type, T4 = boost::spirit::unused_type, boost::spirit::qi::rule<Iterator, T1, T2, T3, T4> = boost::spirit::qi::rule<__gnu_cxx::__normal_iterator<const char*, std::basic_string<char> >, Node(), boost::spirit::unused_type, boost::spirit::unused_type, boost::spirit::unused_type>]’
tt.cc:21:5: instantiated from ‘My_parser<Iterator>::My_parser() [with Iterator = __gnu_cxx::__normal_iterator<const char*, std::basic_string<char> >]’
tt.cc:34:42: instantiated from here
/usr/include/boost/spirit/home/support/container.hpp:262:13: error: no matching function for call to ‘std::basic_string<char>::insert(std::basic_string<char>::iterator, const Node&)’
/usr/include/boost/spirit/home/support/container.hpp:262:13: note: candidates are:
/usr/include/c++/4.6/bits/basic_string.h:1174:7: note: void std::basic_string<_CharT, _Traits, _Alloc>::insert(std::basic_string<_CharT, _Traits, _Alloc>::iterator, std::basic_string<_CharT, _Traits, _Alloc>::size_type, _CharT) [with _CharT = char, _Traits = std::char_traits<char>, _Alloc = std::allocator<char>, std::basic_string<_CharT, _Traits, _Alloc>::iterator = __gnu_cxx::__normal_iterator<char*, std::basic_string<char> >, typename _Alloc::rebind<_CharT>::other::pointer = char*, std::basic_string<_CharT, _Traits, _Alloc>::size_type = long unsigned int]
/usr/include/c++/4.6/bits/basic_string.h:1174:7: note: candidate expects 3 arguments, 2 provided
/usr/include/c++/4.6/bits/basic_string.h:1190:9: note: template<class _InputIterator> void std::basic_string::insert(std::basic_string<_CharT, _Traits, _Alloc>::iterator, _InputIterator, _InputIterator) [with _InputIterator = _InputIterator, _CharT = char, _Traits = std::char_traits<char>, _Alloc = std::allocator<char>, std::basic_string<_CharT, _Traits, _Alloc>::iterator = __gnu_cxx::__normal_iterator<char*, std::basic_string<char> >, typename _Alloc::rebind<_CharT>::other::pointer = char*]
/usr/include/c++/4.6/bits/basic_string.h:1220:7: note: std::basic_string<_CharT, _Traits, _Alloc>& std::basic_string<_CharT, _Traits, _Alloc>::insert(std::basic_string<_CharT, _Traits, _Alloc>::size_type, const std::basic_string<_CharT, _Traits, _Alloc>&) [with _CharT = char, _Traits = std::char_traits<char>, _Alloc = std::allocator<char>, std::basic_string<_CharT, _Traits, _Alloc> = std::basic_string<char>, std::basic_string<_CharT, _Traits, _Alloc>::size_type = long unsigned int]
/usr/include/c++/4.6/bits/basic_string.h:1220:7: note: no known conversion for argument 1 from ‘std::basic_string<char>::iterator {aka __gnu_cxx::__normal_iterator<char*, std::basic_string<char> >}’ to ‘long unsigned int’
/usr/include/c++/4.6/bits/basic_string.h:1242:7: note: std::basic_string<_CharT, _Traits, _Alloc>& std::basic_string<_CharT, _Traits, _Alloc>::insert(std::basic_string<_CharT, _Traits, _Alloc>::size_type, const std::basic_string<_CharT, _Traits, _Alloc>&, std::basic_string<_CharT, _Traits, _Alloc>::size_type, std::basic_string<_CharT, _Traits, _Alloc>::size_type) [with _CharT = char, _Traits = std::char_traits<char>, _Alloc = std::allocator<char>, std::basic_string<_CharT, _Traits, _Alloc> = std::basic_string<char>, std::basic_string<_CharT, _Traits, _Alloc>::size_type = long unsigned int]
/usr/include/c++/4.6/bits/basic_string.h:1242:7: note: candidate expects 4 arguments, 2 provided
/usr/include/c++/4.6/bits/basic_string.tcc:361:6: note: std::basic_string<_CharT, _Traits, _Alloc>& std::basic_string<_CharT, _Traits, _Alloc>::insert(std::basic_string<_CharT, _Traits, _Alloc>::size_type, const _CharT*, std::basic_string<_CharT, _Traits, _Alloc>::size_type) [with _CharT = char, _Traits = std::char_traits<char>, _Alloc = std::allocator<char>, std::basic_string<_CharT, _Traits, _Alloc>::size_type = long unsigned int]
/usr/include/c++/4.6/bits/basic_string.tcc:361:6: note: candidate expects 3 arguments, 2 provided
/usr/include/c++/4.6/bits/basic_string.h:1283:7: note: std::basic_string<_CharT, _Traits, _Alloc>& std::basic_string<_CharT, _Traits, _Alloc>::insert(std::basic_string<_CharT, _Traits, _Alloc>::size_type, const _CharT*) [with _CharT = char, _Traits = std::char_traits<char>, _Alloc = std::allocator<char>, std::basic_string<_CharT, _Traits, _Alloc> = std::basic_string<char>, std::basic_string<_CharT, _Traits, _Alloc>::size_type = long unsigned int]
/usr/include/c++/4.6/bits/basic_string.h:1283:7: note: no known conversion for argument 1 from ‘std::basic_string<char>::iterator {aka __gnu_cxx::__normal_iterator<char*, std::basic_string<char> >}’ to ‘long unsigned int’
/usr/include/c++/4.6/bits/basic_string.h:1306:7: note: std::basic_string<_CharT, _Traits, _Alloc>& std::basic_string<_CharT, _Traits, _Alloc>::insert(std::basic_string<_CharT, _Traits, _Alloc>::size_type, std::basic_string<_CharT, _Traits, _Alloc>::size_type, _CharT) [with _CharT = char, _Traits = std::char_traits<char>, _Alloc = std::allocator<char>, std::basic_string<_CharT, _Traits, _Alloc> = std::basic_string<char>, std::basic_string<_CharT, _Traits, _Alloc>::size_type = long unsigned int]
/usr/include/c++/4.6/bits/basic_string.h:1306:7: note: candidate expects 3 arguments, 2 provided
/usr/include/c++/4.6/bits/basic_string.h:1323:7: note: std::basic_string<_CharT, _Traits, _Alloc>::iterator std::basic_string<_CharT, _Traits, _Alloc>::insert(std::basic_string<_CharT, _Traits, _Alloc>::iterator, _CharT) [with _CharT = char, _Traits = std::char_traits<char>, _Alloc = std::allocator<char>, std::basic_string<_CharT, _Traits, _Alloc>::iterator = __gnu_cxx::__normal_iterator<char*, std::basic_string<char> >, typename _Alloc::rebind<_CharT>::other::pointer = char*]
/usr/include/c++/4.6/bits/basic_string.h:1323:7: note: no known conversion for argument 2 from ‘const Node’ to ‘char’
Compiler/boost version
This is ubuntu 12.04
gcc (Ubuntu/Linaro 4.6.3-1ubuntu5) 4.6.3
boost 1.46.
Having said that, I managed to reproduce my question on ubuntu 14.04 (gcc 4.8.2, boost 1.54)
This reminds me a lot of this infamous limitation:
Spirit Qi attribute propagation issue with single-member struct
However, I cannot make it click, and the usual workarounds don't seem to apply. You might report it on the [spirit-general] mailing list.
In the mean time, here's a phoenix workaround:
node_line = " * " >> node [qi::_val=qi::_1];
See it Live On Coliru
Related
This question already has answers here:
error: no match for ‘operator<<’ (operand types are ‘std::ostream’ {aka ‘std::basic_ostream<char>’} and ‘std::_List_iterator<int>’)
(4 answers)
Why should I not #include <bits/stdc++.h>?
(9 answers)
Closed 8 months ago.
I'm trying to make a program that converts text you put in to bigger ascii art text. I made a prototype and ran it, and I got this error
error: no match for 'operator<<' (operand types are 'std::ostream' {aka 'std::basic_ostream<char>'} and 'std::map<std::__cxx11::basic_string<char>, std::__cxx11::basic_string<char> >::iterator' {aka 'std::_Rb_tree<std::__cxx11::basic_string<char>, std::pair<const std::__cxx11::basic_string<char>, std::__cxx11::basic_string<char> >, std::_Select1st<std::pair<const std::__cxx11::basic_string<char>, std::__cxx11::basic_string<char> > >, std::less<std::__cxx11::basic_string<char> >, std::allocator<std::pair<const std::__cxx11::basic_string<char>, std::__cxx11::basic_string<char> > > >::iterator'})
(The full error is way too large to put here)
I think it's because I'm using a very specific type that std::cout doesn't support. I've tried tons of different ways to convert it into a normal string, but all of the functions I've tried don't support it either.
Code:
#include <map>
#include <string>
#include <iostream>
#include <vector>
#include <bits/stdc++.h>
int main() {
std::map<std::string, std::string> bigabc = {{"A", " ||||| \n|| || \n||||||| \n|| ||"}, {"B", "|||||||\n| |\n||||||\n| |\n|||||||"}, {"C", "|||||||\n||\n||\n||\n|||||||"}};
std::string input;
std::cin >> input;
std::transform(input.begin(), input.end(), input.begin(), ::toupper);
std::vector<std::string> chars(input.begin(), input.end());
for (int pos = 0; pos < chars.size(); pos++) {
std::cout << bigabc.find(chars[pos]) << "\n\n";
}
}
I've been stuck on this for hours. Is there any way to make this work, or should I just rewrite my code entirely?
I tried using bigabc.at() instead and got this error:
In file included from C:/TDM-GCC-64/lib/gcc/x86_64-w64-mingw32/10.3.0/include/c++/vector:66,
from asciisnake.cpp:4:
C:/TDM-GCC-64/lib/gcc/x86_64-w64-mingw32/10.3.0/include/c++/bits/stl_uninitialized.h: In instantiation of '_ForwardIterator std::uninitialized_copy(_InputIterator, _InputIterator, _ForwardIterator) [with _InputIterator = __gnu_cxx::__normal_iterator<char*, std::__cxx11::basic_string<char> >; _ForwardIterator = std::__cxx11::basic_string<char>*]':
C:/TDM-GCC-64/lib/gcc/x86_64-w64-mingw32/10.3.0/include/c++/bits/stl_uninitialized.h:325:37: required from '_ForwardIterator std::__uninitialized_copy_a(_InputIterator, _InputIterator, _ForwardIterator, std::allocator<_Tp>&) [with _InputIterator = __gnu_cxx::__normal_iterator<char*, std::__cxx11::basic_string<char> >; _ForwardIterator = std::__cxx11::basic_string<char>*; _Tp = std::__cxx11::basic_string<char>]'
C:/TDM-GCC-64/lib/gcc/x86_64-w64-mingw32/10.3.0/include/c++/bits/stl_vector.h:1585:33: required from 'void std::vector<_Tp, _Alloc>::_M_range_initialize(_ForwardIterator, _ForwardIterator, std::forward_iterator_tag) [with _ForwardIterator = __gnu_cxx::__normal_iterator<char*, std::__cxx11::basic_string<char> >; _Tp = std::__cxx11::basic_string<char>; _Alloc = std::allocator<std::__cxx11::basic_string<char> >]'
C:/TDM-GCC-64/lib/gcc/x86_64-w64-mingw32/10.3.0/include/c++/bits/stl_vector.h:657:23: required from 'std::vector<_Tp, _Alloc>::vector(_InputIterator, _InputIterator, const allocator_type&) [with _InputIterator = __gnu_cxx::__normal_iterator<char*, std::__cxx11::basic_string<char> >; <template-parameter-2-2> = void; _Tp = std::__cxx11::basic_string<char>; _Alloc = std::allocator<std::__cxx11::basic_string<char> >; std::vector<_Tp, _Alloc>::allocator_type = std::allocator<std::__cxx11::basic_string<char> >]'
asciisnake.cpp:15:62: required from here
C:/TDM-GCC-64/lib/gcc/x86_64-w64-mingw32/10.3.0/include/c++/bits/stl_uninitialized.h:137:72: error: static assertion failed: result type must be constructible from value type of input range
137 | static_assert(is_constructible<_ValueType2, decltype(*__first)>::value,
|
Tried using const map, <char, string> and bigabc[chars[pos]] and got this error:
asciisnake.cpp: In function 'int main()':
asciisnake.cpp:20:23: error: no match for 'operator[]' (operand types are 'const std::map<char, std::__cxx11::basic_string<char> >' and '__gnu_cxx::__alloc_traits<std::allocator<std::__cxx11::basic_string<char> >, std::__cxx11::basic_string<char> >::value_type' {aka 'std::__cxx11::basic_string<char>'})
20 | cout << bigabc[chars[pos]] << "\n\n";
| ^
In file included from C:/TDM-GCC-64/lib/gcc/x86_64-w64-mingw32/10.3.0/include/c++/map:61,
from asciisnake.cpp:1:
C:/TDM-GCC-64/lib/gcc/x86_64-w64-mingw32/10.3.0/include/c++/bits/stl_map.h:492:7: note: candidate: 'std::map<_Key, _Tp, _Compare, _Alloc>::mapped_type& std::map<_Key, _Tp, _Compare, _Alloc>::operator[](const key_type&) [with _Key = char; _Tp = std::__cxx11::basic_string<char>; _Compare = std::less<char>; _Alloc = std::allocator<std::pair<const char, std::__cxx11::basic_string<char> > >; std::map<_Key, _Tp, _Compare, _Alloc>::mapped_type = std::__cxx11::basic_string<char>; std::map<_Key, _Tp, _Compare, _Alloc>::key_type = char]'
492 | operator[](const key_type& __k)
| ^~~~~~~~
C:/TDM-GCC-64/lib/gcc/x86_64-w64-mingw32/10.3.0/include/c++/bits/stl_map.h:492:34: note: no known conversion for argument 1 from '__gnu_cxx::__alloc_traits<std::allocator<std::__cxx11::basic_string<char> >, std::__cxx11::basic_string<char> >::value_type' {aka 'std::__cxx11::basic_string<char>'} to 'const key_type&' {aka 'const char&'}
492 | operator[](const key_type& __k)
| ~~~~~~~~~~~~~~~~^~~
C:/TDM-GCC-64/lib/gcc/x86_64-w64-mingw32/10.3.0/include/c++/bits/stl_map.h:512:7: note: candidate: 'std::map<_Key, _Tp, _Compare, _Alloc>::mapped_type& std::map<_Key, _Tp, _Compare, _Alloc>::operator[](std::map<_Key, _Tp, _Compare, _Alloc>::key_type&&) [with _Key = char; _Tp = std::__cxx11::basic_string<char>; _Compare = std::less<char>; _Alloc = std::allocator<std::pair<const char, std::__cxx11::basic_string<char> > >; std::map<_Key, _Tp, _Compare, _Alloc>::mapped_type = std::__cxx11::basic_string<char>; std::map<_Key, _Tp, _Compare, _Alloc>::key_type = char]'
512 | operator[](key_type&& __k)
| ^~~~~~~~
C:/TDM-GCC-64/lib/gcc/x86_64-w64-mingw32/10.3.0/include/c++/bits/stl_map.h:512:29: note: no known conversion for argument 1 from '__gnu_cxx::__alloc_traits<std::allocator<std::__cxx11::basic_string<char> >, std::__cxx11::basic_string<char> >::value_type' {aka 'std::__cxx11::basic_string<char>'} to 'std::map<char, std::__cxx11::basic_string<char> >::key_type&&' {aka 'char&&'}
512 | operator[](key_type&& __k)
| ~~~~~~~~~~~^~~
In file included from C:/TDM-GCC-64/lib/gcc/x86_64-w64-mingw32/10.3.0/include/c++/vector:66,
from asciisnake.cpp:4:
C:/TDM-GCC-64/lib/gcc/x86_64-w64-mingw32/10.3.0/include/c++/bits/stl_uninitialized.h: In instantiation of '_ForwardIterator std::uninitialized_copy(_InputIterator, _InputIterator, _ForwardIterator) [with _InputIterator = __gnu_cxx::__normal_iterator<char*, std::__cxx11::basic_string<char> >; _ForwardIterator = std::__cxx11::basic_string<char>*]':
C:/TDM-GCC-64/lib/gcc/x86_64-w64-mingw32/10.3.0/include/c++/bits/stl_uninitialized.h:325:37: required from '_ForwardIterator std::__uninitialized_copy_a(_InputIterator, _InputIterator, _ForwardIterator, std::allocator<_Tp>&) [with _InputIterator = __gnu_cxx::__normal_iterator<char*, std::__cxx11::basic_string<char> >; _ForwardIterator = std::__cxx11::basic_string<char>*; _Tp = std::__cxx11::basic_string<char>]'
C:/TDM-GCC-64/lib/gcc/x86_64-w64-mingw32/10.3.0/include/c++/bits/stl_vector.h:1585:33: required from 'void std::vector<_Tp, _Alloc>::_M_range_initialize(_ForwardIterator, _ForwardIterator, std::forward_iterator_tag) [with _ForwardIterator = __gnu_cxx::__normal_iterator<char*, std::__cxx11::basic_string<char> >; _Tp = std::__cxx11::basic_string<char>; _Alloc = std::allocator<std::__cxx11::basic_string<char> >]'
C:/TDM-GCC-64/lib/gcc/x86_64-w64-mingw32/10.3.0/include/c++/bits/stl_vector.h:657:23: required from 'std::vector<_Tp, _Alloc>::vector(_InputIterator, _InputIterator, const allocator_type&) [with _InputIterator = __gnu_cxx::__normal_iterator<char*, std::__cxx11::basic_string<char> >; <template-parameter-2-2> = void; _Tp = std::__cxx11::basic_string<char>; _Alloc = std::allocator<std::__cxx11::basic_string<char> >; std::vector<_Tp, _Alloc>::allocator_type = std::allocator<std::__cxx11::basic_string<char> >]'
asciisnake.cpp:17:52: required from here
C:/TDM-GCC-64/lib/gcc/x86_64-w64-mingw32/10.3.0/include/c++/bits/stl_uninitialized.h:137:72: error: static assertion failed: result type must be constructible from value type of input range
137 | static_assert(is_constructible<_ValueType2, decltype(*__first)>::value,
|
bigabc.find() will return an iterator pointing to the matching pair {"A","||......"}. You can use ->second to get the second string, and that will print fine.
Even easier, you can use the [] operator instead of find(): cout << bigabc[chars[pos]]; will print the matching 'big' string for chars[pos].
Let the code speak it:
auto SetCompare = [](const string& a, const string& b)
{
size_t L = a.length();
size_t R = b.length();
if (L == R) return (a < b);
return L < R;
};
using MySet = std::set<string, decltype(SetCompare)>;
unordered_map<string, MySet> Map;
The insertion or access won't work:
Map["abc"];
Map["xyz"].insert("mapped to xyz");
// Insert TO set works
MySet mySet(SetCompare); // HINT
mySet.insert("x");
mySet.insert("abc");
// But not to the map!
Map.insert({"pqr", mySet});
At the HINTed location, I pass SetCompare lambda (not just type) to MySet's constructor. The question is how to pass it to the "Value" type of unordered_map?
EDIT (Compiler errors):
GCC C++17
In file included from /usr/include/c++/7/set:60:0,
from main.cpp:10:
/usr/include/c++/7/bits/stl_tree.h: In instantiation of ‘std::_Rb_tree_key_compare<_Key_compare>::_Rb_tree_key_compare() [with _Key_compare = main()::<lambda(const string&, const string&)>]’:
/usr/include/c++/7/bits/stl_tree.h:688:4: required from ‘std::pair<_T1, _T2>::pair(std::tuple<_Args1 ...>&, std::tuple<_Args2 ...>&, std::_Index_tuple<_Indexes1 ...>, std::_Index_tuple<_Indexes2 ...>) [with _Args1 = {char&&}; long unsigned int ..._Indexes1 = {0}; _Args2 = {}; long unsigned int ..._Indexes2 = {}; _T1 = const char; _T2 = std::set<std::basic_string<char>, main()::<lambda(const string&, const string&)> >]’
/usr/include/c++/7/tuple:1641:63: required from ‘std::pair<_T1, _T2>::pair(std::piecewise_construct_t, std::tuple<_Args1 ...>, std::tuple<_Args2 ...>) [with _Args1 = {char&&}; _Args2 = {}; _T1 = const char; _T2 = std::set<std::basic_string<char>, main()::<lambda(const string&, const string&)> >]’
/usr/include/c++/7/ext/new_allocator.h:136:4: required from ‘void __gnu_cxx::new_allocator<_Tp>::construct(_Up*, _Args&& ...) [with _Up = std::pair<const char, std::set<std::basic_string<char>, main()::<lambda(const string&, const string&)> > >; _Args = {const std::piecewise_construct_t&, std::tuple<char&&>, std::tuple<>}; _Tp = std::pair<const char, std::set<std::basic_string<char>, main()::<lambda(const string&, const string&)> > >]’
/usr/include/c++/7/bits/alloc_traits.h:475: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 char, std::set<std::basic_string<char>, main()::<lambda(const string&, const string&)> > >; _Args = {const std::piecewise_construct_t&, std::tuple<char&&>, std::tuple<>}; _Tp = std::pair<const char, std::set<std::basic_string<char>, main()::<lambda(const string&, const string&)> > >; std::allocator_traits<std::allocator<_CharT> >::allocator_type = std::allocator<std::pair<const char, std::set<std::basic_string<char>, main()::<lambda(const string&, const string&)> > > >]’
/usr/include/c++/7/bits/hashtable_policy.h:2066:37: required from ‘std::__detail::_Hashtable_alloc<_NodeAlloc>::__node_type* std::__detail::_Hashtable_alloc<_NodeAlloc>::_M_allocate_node(_Args&& ...) [with _Args = {const std::piecewise_construct_t&, std::tuple<char&&>, std::tuple<>}; _NodeAlloc = std::allocator<std::__detail::_Hash_node<std::pair<const char, std::set<std::basic_string<char>, main()::<lambda(const string&, const string&)> > >, false> >; std::__detail::_Hashtable_alloc<_NodeAlloc>::__node_type = std::__detail::_Hash_node<std::pair<const char, std::set<std::basic_string<char>, main()::<lambda(const string&, const string&)> > >, false>]’
/usr/include/c++/7/bits/hashtable_policy.h:750:8: required from ‘std::__detail::_Map_base<_Key, _Pair, _Alloc, std::__detail::_Select1st, _Equal, _H1, _H2, _Hash, _RehashPolicy, _Traits, true>::mapped_type& std::__detail::_Map_base<_Key, _Pair, _Alloc, std::__detail::_Select1st, _Equal, _H1, _H2, _Hash, _RehashPolicy, _Traits, true>::operator[](std::__detail::_Map_base<_Key, _Pair, _Alloc, std::__detail::_Select1st, _Equal, _H1, _H2, _Hash, _RehashPolicy, _Traits, true>::key_type&&) [with _Key = char; _Pair = std::pair<const char, std::set<std::basic_string<char>, main()::<lambda(const string&, const string&)> > >; _Alloc = std::allocator<std::pair<const char, std::set<std::basic_string<char>, main()::<lambda(const string&, const string&)> > > >; _Equal = std::equal_to<char>; _H1 = std::hash<char>; _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::_Map_base<_Key, _Pair, _Alloc, std::__detail::_Select1st, _Equal, _H1, _H2, _Hash, _RehashPolicy, _Traits, true>::mapped_type = std::set<std::basic_string<char>, main()::<lambda(const string&, const string&)> >; std::__detail::_Map_base<_Key, _Pair, _Alloc, std::__detail::_Select1st, _Equal, _H1, _H2, _Hash, _RehashPolicy, _Traits, true>::key_type = char]’
/usr/include/c++/7/bits/unordered_map.h:977:20: required from ‘std::unordered_map<_Key, _Tp, _Hash, _Pred, _Alloc>::mapped_type& std::unordered_map<_Key, _Tp, _Hash, _Pred, _Alloc>::operator[](std::unordered_map<_Key, _Tp, _Hash, _Pred, _Alloc>::key_type&&) [with _Key = char; _Tp = std::set<std::basic_string<char>, main()::<lambda(const string&, const string&)> >; _Hash = std::hash<char>; _Pred = std::equal_to<char>; _Alloc = std::allocator<std::pair<const char, std::set<std::basic_string<char>, main()::<lambda(const string&, const string&)> > > >; std::unordered_map<_Key, _Tp, _Hash, _Pred, _Alloc>::mapped_type = std::set<std::basic_string<char>, main()::<lambda(const string&, const string&)> >; std::unordered_map<_Key, _Tp, _Hash, _Pred, _Alloc>::key_type = char]’
<span class="error_line" onclick="ide.gotoLine('main.cpp',31)">main.cpp:31:7</span>: required from here
/usr/include/c++/7/bits/stl_tree.h:149:24: error: use of deleted function ‘main()::::()’
: _M_key_compare()
^
main.cpp:17:24: note: a lambda closure type has a deleted default constructor
auto SetCompare = [](const string& a, const string& b)
GCC C++17
In file included from /usr/include/c++/6/set:60:0,
from main.cpp:10:
/usr/include/c++/6/bits/stl_tree.h: In instantiation of ‘std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::_Rb_tree_impl<_Key_compare, <anonymous> >::_Rb_tree_impl() [with _Key_compare = main()::<lambda(const string&, const string&)>; bool <anonymous> = false; _Key = std::basic_string<char>; _Val = std::basic_string<char>; _KeyOfValue = std::_Identity<std::basic_string<char> >; _Compare = main()::<lambda(const string&, const string&)>; _Alloc = std::allocator<std::basic_string<char> >]’:
/usr/include/c++/6/bits/stl_tree.h:821:18: required from ‘std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::_Rb_tree() [with _Key = std::basic_string<char>; _Val = std::basic_string<char>; _KeyOfValue = std::_Identity<std::basic_string<char> >; _Compare = main()::<lambda(const string&, const string&)>; _Alloc = std::allocator<std::basic_string<char> >]’
/usr/include/c++/6/bits/stl_set.h:146:14: required from ‘std::set<_Key, _Compare, _Alloc>::set() [with _Key = std::basic_string<char>; _Compare = main()::<lambda(const string&, const string&)>; _Alloc = std::allocator<std::basic_string<char> >]’
/usr/include/c++/6/tuple:1590:70: required from ‘std::pair<_T1, _T2>::pair(std::tuple<_Args1 ...>&, std::tuple<_Args2 ...>&, std::_Index_tuple<_Indexes1 ...>, std::_Index_tuple<_Indexes2 ...>) [with _Args1 = {char&&}; long unsigned int ..._Indexes1 = {0ul}; _Args2 = {}; long unsigned int ..._Indexes2 = {}; _T1 = const char; _T2 = std::set<std::basic_string<char>, main()::<lambda(const string&, const string&)> >]’
/usr/include/c++/6/tuple:1579:63: required from ‘std::pair<_T1, _T2>::pair(std::piecewise_construct_t, std::tuple<_Args1 ...>, std::tuple<_Args2 ...>) [with _Args1 = {char&&}; _Args2 = {}; _T1 = const char; _T2 = std::set<std::basic_string<char>, main()::<lambda(const string&, const string&)> >]’
/usr/include/c++/6/ext/new_allocator.h:120:4: required from ‘void __gnu_cxx::new_allocator<_Tp>::construct(_Up*, _Args&& ...) [with _Up = std::pair<const char, std::set<std::basic_string<char>, main()::<lambda(const string&, const string&)> > >; _Args = {const std::piecewise_construct_t&, std::tuple<char&&>, std::tuple<>}; _Tp = std::pair<const char, std::set<std::basic_string<char>, main()::<lambda(const string&, const string&)> > >]’
/usr/include/c++/6/bits/alloc_traits.h:475: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 char, std::set<std::basic_string<char>, main()::<lambda(const string&, const string&)> > >; _Args = {const std::piecewise_construct_t&, std::tuple<char&&>, std::tuple<>}; _Tp = std::pair<const char, std::set<std::basic_string<char>, main()::<lambda(const string&, const string&)> > >; std::allocator_traits<std::allocator<_CharT> >::allocator_type = std::allocator<std::pair<const char, std::set<std::basic_string<char>, main()::<lambda(const string&, const string&)> > > >]’
/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::piecewise_construct_t&, std::tuple<char&&>, std::tuple<>}; _NodeAlloc = std::allocator<std::__detail::_Hash_node<std::pair<const char, std::set<std::basic_string<char>, main()::<lambda(const string&, const string&)> > >, false> >; std::__detail::_Hashtable_alloc<_NodeAlloc>::__node_type = std::__detail::_Hash_node<std::pair<const char, std::set<std::basic_string<char>, main()::<lambda(const string&, const string&)> > >, false>]’
/usr/include/c++/6/bits/hashtable_policy.h:620:8: required from ‘std::__detail::_Map_base<_Key, _Pair, _Alloc, std::__detail::_Select1st, _Equal, _H1, _H2, _Hash, _RehashPolicy, _Traits, true>::mapped_type& std::__detail::_Map_base<_Key, _Pair, _Alloc, std::__detail::_Select1st, _Equal, _H1, _H2, _Hash, _RehashPolicy, _Traits, true>::operator[](std::__detail::_Map_base<_Key, _Pair, _Alloc, std::__detail::_Select1st, _Equal, _H1, _H2, _Hash, _RehashPolicy, _Traits, true>::key_type&&) [with _Key = char; _Pair = std::pair<const char, std::set<std::basic_string<char>, main()::<lambda(const string&, const string&)> > >; _Alloc = std::allocator<std::pair<const char, std::set<std::basic_string<char>, main()::<lambda(const string&, const string&)> > > >; _Equal = std::equal_to<char>; _H1 = std::hash<char>; _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::_Map_base<_Key, _Pair, _Alloc, std::__detail::_Select1st, _Equal, _H1, _H2, _Hash, _RehashPolicy, _Traits, true>::mapped_type = std::set<std::basic_string<char>, main()::<lambda(const string&, const string&)> >; std::__detail::_Map_base<_Key, _Pair, _Alloc, std::__detail::_Select1st, _Equal, _H1, _H2, _Hash, _RehashPolicy, _Traits, true>::key_type = char]’
/usr/include/c++/6/bits/unordered_map.h:908:20: required from ‘std::unordered_map<_Key, _Tp, _Hash, _Pred, _Alloc>::mapped_type& std::unordered_map<_Key, _Tp, _Hash, _Pred, _Alloc>::operator[](std::unordered_map<_Key, _Tp, _Hash, _Pred, _Alloc>::key_type&&) [with _Key = char; _Tp = std::set<std::basic_string<char>, main()::<lambda(const string&, const string&)> >; _Hash = std::hash<char>; _Pred = std::equal_to<char>; _Alloc = std::allocator<std::pair<const char, std::set<std::basic_string<char>, main()::<lambda(const string&, const string&)> > > >; std::unordered_map<_Key, _Tp, _Hash, _Pred, _Alloc>::mapped_type = std::set<std::basic_string<char>, main()::<lambda(const string&, const string&)> >; std::unordered_map<_Key, _Tp, _Hash, _Pred, _Alloc>::key_type = char]’
<span class="error_line" onclick="ide.gotoLine('main.cpp',31)">main.cpp:31:7</span>: required from here
/usr/include/c++/6/bits/stl_tree.h:602:21: error: use of deleted function ‘main()::::()’
_M_node_count(0)
^
main.cpp:17:24: note: a lambda closure type has a deleted default constructor
auto SetCompare = [](const string& a, const string& b)
^
In file included from /usr/include/c++/6/set:60:0,
from main.cpp:10:
/usr/include/c++/6/bits/stl_tree.h:628:4: warning: ‘void std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::_Rb_tree_impl<_Key_compare, >::_M_initialize() [with _Key_compare = main()::; bool = false; _Key = std::basic_string; _Val = std::basic_string; _KeyOfValue = std::_Identity >; _Compare = main()::; _Alloc = std::allocator >]’ used but never defined
_M_initialize()
^~~~~~~~~~~~~
VC C++14/17
1>C:\Program Files (x86)\Microsoft Visual Studio\2019\Professional\VC\Tools\MSVC\14.27.29110\include\set(84,1): error C2280: 'main::<lambda_b1395bcc88464fd844969fa0764f137c>::<lambda_b1395bcc88464fd844969fa0764f137c>(void)': attempting to reference a deleted function
1>xx.cpp(923): message : see declaration of 'main::<lambda_b1395bcc88464fd844969fa0764f137c>::<lambda_b1395bcc88464fd844969fa0764f137c>'
1>xxx.cpp(923,20): message : 'main::<lambda_b1395bcc88464fd844969fa0764f137c>::<lambda_b1395bcc88464fd844969fa0764f137c>(void)': function was explicitly deleted
1>C:\Program Files (x86)\Microsoft Visual Studio\2019\Professional\VC\Tools\MSVC\14.27.29110\include\set(84): message : while compiling class template member function 'std::set<std::string,main::<lambda_b1395bcc88464fd844969fa0764f137c>,std::allocator<std::string>>::set(void)'
1>C:\Program Files (x86)\Microsoft Visual Studio\2019\Professional\VC\Tools\MSVC\14.27.29110\include\tuple(975): message : see reference to function template instantiation 'std::set<std::string,main::<lambda_b1395bcc88464fd844969fa0764f137c>,std::allocator<std::string>>::set(void)' being compiled
1>xxx.cpp(937): message : see reference to class template instantiation 'std::set<std::string,main::<lambda_b1395bcc88464fd844969fa0764f137c>,std::allocator<std::string>>' being compiled
Lambdas are never default constructible before C++20 (see ClosureType::ClosureType() section on cppreference), so you must pass the SetCompare variable when constructing a MySet. Using -std=c++20/-std=c++2a on clang or GCC should work (demo), as well as /std:c++latest on MSVC.
To work around this limitation in earlier C++ versions define SetCompare as a functor type:
struct SetCompare
{
bool operator()(const string& a, const string& b)
{
size_t L = a.length();
size_t R = b.length();
if (L == R) return (a < b);
return L < R;
}
};
and use this type when defining MySet
using MySet = std::set<std::string, SetCompare>;
This code works in GCC 6.1, but causes a compile error in GCC 5.5:
#include <memory>
#include <unordered_map>
#include <vector>
std::unordered_map<int, std::shared_ptr<std::vector<uint8_t>>> foo;
int main() {
foo.emplace(0, new std::vector<uint8_t>(1));
}
The error is below. Is there any way around this (I assume) bug?
In file included from /opt/compiler-explorer/gcc-5.5.0/include/c++/5.5.0/x86_64-linux-gnu/bits/c++allocator.h:33:0,
from /opt/compiler-explorer/gcc-5.5.0/include/c++/5.5.0/bits/allocator.h:46,
from /opt/compiler-explorer/gcc-5.5.0/include/c++/5.5.0/memory:63,
from <source>:1:
/opt/compiler-explorer/gcc-5.5.0/include/c++/5.5.0/ext/new_allocator.h: In instantiation of 'void
__gnu_cxx::new_allocator<_Tp>::construct(_Up*, _Args&& ...) [with _Up = std::pair<const int, std::shared_ptr<std::vector<unsigned char> > >; _Args = {int, std::vector<unsigned char, std::allocator<unsigned char> >*}; _Tp = std::pair<const int, std::shared_ptr<std::vector<unsigned char> > >]':
/opt/compiler-explorer/gcc-5.5.0/include/c++/5.5.0/bits/alloc_traits.h:530:4: required from 'static void std::allocator_traits<std::allocator<_Tp1>
>::construct(std::allocator_traits<std::allocator<_Tp1> >::allocator_type&, _Up*, _Args&& ...) [with _Up = std::pair<const int, std::shared_ptr<std::vector<unsigned char> > >; _Args = {int, std::vector<unsigned char, std::allocator<unsigned char> >*}; _Tp = std::pair<const int, std::shared_ptr<std::vector<unsigned char> > >; std::allocator_traits<std::allocator<_Tp1> >::allocator_type = std::allocator<std::pair<const int, std::shared_ptr<std::vector<unsigned char> > > >]'
/opt/compiler-explorer/gcc-5.5.0/include/c++/5.5.0/bits/hashtable_policy.h:1955:37: required from 'std::__detail::_Hashtable_alloc<_NodeAlloc>::__node_type* std::__detail::_Hashtable_alloc<_NodeAlloc>::_M_allocate_node(_Args&& ...) [with _Args = {int, std::vector<unsigned char, std::allocator<unsigned char> >*}; _NodeAlloc = std::allocator<std::__detail::_Hash_node<std::pair<const int, std::shared_ptr<std::vector<unsigned char> > >, false> >; std::__detail::_Hashtable_alloc<_NodeAlloc>::__node_type = std::__detail::_Hash_node<std::pair<const int, std::shared_ptr<std::vector<unsigned char> > >, false>]'
/opt/compiler-explorer/gcc-5.5.0/include/c++/5.5.0/bits/hashtable.h:1517:77: 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_emplace(std::true_type, _Args&& ...) [with _Args = {int, std::vector<unsigned char, std::allocator<unsigned char> >*}; _Key = int; _Value = std::pair<const int, std::shared_ptr<std::vector<unsigned char> > >;
_Alloc = std::allocator<std::pair<const int, std::shared_ptr<std::vector<unsigned char> > > >; _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::shared_ptr<std::vector<unsigned char> > >, false, false>; std::true_type = std::integral_constant<bool, true>]'
/opt/compiler-explorer/gcc-5.5.0/include/c++/5.5.0/bits/hashtable.h:726:21: required from 'std::_Hashtable<_Key, _Value, _Alloc, _ExtractKey,
_Equal, _H1, _H2, _Hash, _RehashPolicy, _Traits>::__ireturn_type std::_Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal, _H1, _H2,
_Hash, _RehashPolicy, _Traits>::emplace(_Args&& ...) [with _Args = {int, std::vector<unsigned char, std::allocator<unsigned char> >*};
_Key = int; _Value = std::pair<const int, std::shared_ptr<std::vector<unsigned char> > >; _Alloc = std::allocator<std::pair<const int, std::shared_ptr<std::vector<unsigned char> > > >; _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::_Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal, _H1, _H2,
_Hash, _RehashPolicy, _Traits>::__ireturn_type = std::pair<std::__detail::_Node_iterator<std::pair<const int, std::shared_ptr<std::vector<unsigned char> > >, false, false>, bool>]'
/opt/compiler-explorer/gcc-5.5.0/include/c++/5.5.0/bits/unordered_map.h:380:54: 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>::emplace(_Args&& ...) [with _Args = {int, std::vector<unsigned char, std::allocator<unsigned char> >*}; _Key = int; _Tp = std::shared_ptr<std::vector<unsigned char> >; _Hash = std::hash<int>;
_Pred = std::equal_to<int>; _Alloc = std::allocator<std::pair<const int, std::shared_ptr<std::vector<unsigned char> > > >; 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::shared_ptr<std::vector<unsigned char> > >, false, false>]'
<source>:8:47: required from here
/opt/compiler-explorer/gcc-5.5.0/include/c++/5.5.0/ext/new_allocator.h:120:4: error: no matching function for call to 'std::pair<const int, std::shared_ptr<std::vector<unsigned char> > >::pair(int, std::vector<unsigned char>*)'
{ ::new((void *)__p) _Up(std::forward<_Args>(__args)...); }
^
In file included from /opt/compiler-explorer/gcc-5.5.0/include/c++/5.5.0/bits/stl_algobase.h:64:0,
from /opt/compiler-explorer/gcc-5.5.0/include/c++/5.5.0/memory:62,
from <source>:1:
/opt/compiler-explorer/gcc-5.5.0/include/c++/5.5.0/bits/stl_pair.h:206:9: note: candidate: template<class ... _Args1, long unsigned int ..._Indexes1, class ... _Args2, long unsigned int ..._Indexes2> std::pair<_T1, _T2>::pair(std::tuple<_Args1 ...>&, std::tuple<_Args2 ...>&, std::_Index_tuple<_Indexes1 ...>, std::_Index_tuple<_Indexes2 ...>)
pair(tuple<_Args1...>&, tuple<_Args2...>&,
^
/opt/compiler-explorer/gcc-5.5.0/include/c++/5.5.0/bits/stl_pair.h:206:9: note: template argument deduction/substitution failed:
In file included from /opt/compiler-explorer/gcc-5.5.0/include/c++/5.5.0/x86_64-linux-gnu/bits/c++allocator.h:33:0,
from /opt/compiler-explorer/gcc-5.5.0/include/c++/5.5.0/bits/allocator.h:46,
from /opt/compiler-explorer/gcc-5.5.0/include/c++/5.5.0/memory:63,
from <source>:1:
/opt/compiler-explorer/gcc-5.5.0/include/c++/5.5.0/ext/new_allocator.h:120:4: note: mismatched types 'std::tuple<_Elements ...>' and 'int'
{ ::new((void *)__p) _Up(std::forward<_Args>(__args)...); }
^
In file included from /opt/compiler-explorer/gcc-5.5.0/include/c++/5.5.0/bits/stl_algobase.h:64:0,
from /opt/compiler-explorer/gcc-5.5.0/include/c++/5.5.0/memory:62,
from <source>:1:
/opt/compiler-explorer/gcc-5.5.0/include/c++/5.5.0/bits/stl_pair.h:155:9: note: candidate: template<class ... _Args1, class ... _Args2> std::pair<_T1, _T2>::pair(std::piecewise_construct_t, std::tuple<_Args1 ...>, std::tuple<_Args2 ...>)
pair(piecewise_construct_t, tuple<_Args1...>, tuple<_Args2...>);
^
/opt/compiler-explorer/gcc-5.5.0/include/c++/5.5.0/bits/stl_pair.h:155:9: note: template argument deduction/substitution failed:
In file included from /opt/compiler-explorer/gcc-5.5.0/include/c++/5.5.0/x86_64-linux-gnu/bits/c++allocator.h:33:0,
from /opt/compiler-explorer/gcc-5.5.0/include/c++/5.5.0/bits/allocator.h:46,
from /opt/compiler-explorer/gcc-5.5.0/include/c++/5.5.0/memory:63,
from <source>:1:
/opt/compiler-explorer/gcc-5.5.0/include/c++/5.5.0/ext/new_allocator.h:120:4: note: cannot convert 'std::forward<int>((* & __args#0))' (type 'int') to type 'std::piecewise_construct_t'
{ ::new((void *)__p) _Up(std::forward<_Args>(__args)...); }
^
In file included from /opt/compiler-explorer/gcc-5.5.0/include/c++/5.5.0/bits/stl_algobase.h:64:0,
from /opt/compiler-explorer/gcc-5.5.0/include/c++/5.5.0/memory:62,
from <source>:1:
/opt/compiler-explorer/gcc-5.5.0/include/c++/5.5.0/bits/stl_pair.h:150:12: note: candidate: template<class _U1, class _U2, class> constexpr std::pair<_T1, _T2>::pair(std::pair<_U1, _U2>&&)
constexpr pair(pair<_U1, _U2>&& __p)
^
/opt/compiler-explorer/gcc-5.5.0/include/c++/5.5.0/bits/stl_pair.h:150:12: note: template argument deduction/substitution failed:
In file included from /opt/compiler-explorer/gcc-5.5.0/include/c++/5.5.0/x86_64-linux-gnu/bits/c++allocator.h:33:0,
from /opt/compiler-explorer/gcc-5.5.0/include/c++/5.5.0/bits/allocator.h:46,
from /opt/compiler-explorer/gcc-5.5.0/include/c++/5.5.0/memory:63,
from <source>:1:
/opt/compiler-explorer/gcc-5.5.0/include/c++/5.5.0/ext/new_allocator.h:120:4: note: mismatched types 'std::pair<_T1, _T2>' and 'int'
{ ::new((void *)__p) _Up(std::forward<_Args>(__args)...); }
^
In file included from /opt/compiler-explorer/gcc-5.5.0/include/c++/5.5.0/bits/stl_algobase.h:64:0,
from /opt/compiler-explorer/gcc-5.5.0/include/c++/5.5.0/memory:62,
from <source>:1:
/opt/compiler-explorer/gcc-5.5.0/include/c++/5.5.0/bits/stl_pair.h:144:12: note: candidate: template<class _U1, class _U2, class> constexpr std::pair<_T1, _T2>::pair(_U1&&, _U2&&)
constexpr pair(_U1&& __x, _U2&& __y)
^
/opt/compiler-explorer/gcc-5.5.0/include/c++/5.5.0/bits/stl_pair.h:144:12: note: template argument deduction/substitution failed:
/opt/compiler-explorer/gcc-5.5.0/include/c++/5.5.0/bits/stl_pair.h:141:38: error: no type named 'type' in 'struct std::enable_if<false, void>'
template<class _U1, class _U2, class = typename
^
/opt/compiler-explorer/gcc-5.5.0/include/c++/5.5.0/bits/stl_pair.h:138:12: note: candidate: template<class _U2, class> constexpr std::pair<_T1,
_T2>::pair(const _T1&, _U2&&)
constexpr pair(const _T1& __x, _U2&& __y)
^
/opt/compiler-explorer/gcc-5.5.0/include/c++/5.5.0/bits/stl_pair.h:138:12: note: template argument deduction/substitution failed:
/opt/compiler-explorer/gcc-5.5.0/include/c++/5.5.0/bits/stl_pair.h:136:27: error: no type named 'type' in 'struct std::enable_if<false, void>'
template<class _U2, class = typename
^
/opt/compiler-explorer/gcc-5.5.0/include/c++/5.5.0/bits/stl_pair.h:133:12: note: candidate: template<class _U1, class> constexpr std::pair<_T1,
_T2>::pair(_U1&&, const _T2&)
constexpr pair(_U1&& __x, const _T2& __y)
^
/opt/compiler-explorer/gcc-5.5.0/include/c++/5.5.0/bits/stl_pair.h:133:12: note: template argument deduction/substitution failed:
In file included from /opt/compiler-explorer/gcc-5.5.0/include/c++/5.5.0/x86_64-linux-gnu/bits/c++allocator.h:33:0,
from /opt/compiler-explorer/gcc-5.5.0/include/c++/5.5.0/bits/allocator.h:46,
from /opt/compiler-explorer/gcc-5.5.0/include/c++/5.5.0/memory:63,
from <source>:1:
/opt/compiler-explorer/gcc-5.5.0/include/c++/5.5.0/ext/new_allocator.h:120:4: note: cannot convert 'std::forward<std::vector<unsigned char>*>((* &
__args#1))' (type 'std::vector<unsigned char>*') to type 'const std::shared_ptr<std::vector<unsigned char> >&'
{ ::new((void *)__p) _Up(std::forward<_Args>(__args)...); }
^
In file included from /opt/compiler-explorer/gcc-5.5.0/include/c++/5.5.0/bits/stl_algobase.h:64:0,
from /opt/compiler-explorer/gcc-5.5.0/include/c++/5.5.0/memory:62,
from <source>:1:
/opt/compiler-explorer/gcc-5.5.0/include/c++/5.5.0/bits/stl_pair.h:128:17: note: candidate: constexpr std::pair<_T1, _T2>::pair(std::pair<_T1,
_T2>&&) [with _T1 = const int; _T2 = std::shared_ptr<std::vector<unsigned char> >]
constexpr pair(pair&&) = default;
^
/opt/compiler-explorer/gcc-5.5.0/include/c++/5.5.0/bits/stl_pair.h:128:17: note: candidate expects 1 argument, 2 provided
/opt/compiler-explorer/gcc-5.5.0/include/c++/5.5.0/bits/stl_pair.h:127:17: note: candidate: constexpr std::pair<_T1, _T2>::pair(const std::pair<_T1, _T2>&) [with _T1 = const int; _T2 = std::shared_ptr<std::vector<unsigned char> >]
constexpr pair(const pair&) = default;
^
/opt/compiler-explorer/gcc-5.5.0/include/c++/5.5.0/bits/stl_pair.h:127:17: note: candidate expects 1 argument, 2 provided
/opt/compiler-explorer/gcc-5.5.0/include/c++/5.5.0/bits/stl_pair.h:124:12: note: candidate: template<class _U1, class _U2, class> constexpr std::pair<_T1, _T2>::pair(const std::pair<_U1, _U2>&)
constexpr pair(const pair<_U1, _U2>& __p)
^
/opt/compiler-explorer/gcc-5.5.0/include/c++/5.5.0/bits/stl_pair.h:124:12: note: template argument deduction/substitution failed:
In file included from /opt/compiler-explorer/gcc-5.5.0/include/c++/5.5.0/x86_64-linux-gnu/bits/c++allocator.h:33:0,
from /opt/compiler-explorer/gcc-5.5.0/include/c++/5.5.0/bits/allocator.h:46,
from /opt/compiler-explorer/gcc-5.5.0/include/c++/5.5.0/memory:63,
from <source>:1:
/opt/compiler-explorer/gcc-5.5.0/include/c++/5.5.0/ext/new_allocator.h:120:4: note: mismatched types 'const std::pair<_T1, _T2>' and 'int'
{ ::new((void *)__p) _Up(std::forward<_Args>(__args)...); }
^
In file included from /opt/compiler-explorer/gcc-5.5.0/include/c++/5.5.0/bits/stl_algobase.h:64:0,
from /opt/compiler-explorer/gcc-5.5.0/include/c++/5.5.0/memory:62,
from <source>:1:
/opt/compiler-explorer/gcc-5.5.0/include/c++/5.5.0/bits/stl_pair.h:112:26: note: candidate: constexpr std::pair<_T1, _T2>::pair(const _T1&, const
_T2&) [with _T1 = const int; _T2 = std::shared_ptr<std::vector<unsigned char> >]
_GLIBCXX_CONSTEXPR pair(const _T1& __a, const _T2& __b)
^
/opt/compiler-explorer/gcc-5.5.0/include/c++/5.5.0/bits/stl_pair.h:112:26: note: no known conversion for argument 2 from 'std::vector<unsigned char>*' to 'const std::shared_ptr<std::vector<unsigned char> >&'
/opt/compiler-explorer/gcc-5.5.0/include/c++/5.5.0/bits/stl_pair.h:108:26: note: candidate: constexpr std::pair<_T1, _T2>::pair() [with _T1 = const int; _T2 = std::shared_ptr<std::vector<unsigned char> >]
_GLIBCXX_CONSTEXPR pair()
^
/opt/compiler-explorer/gcc-5.5.0/include/c++/5.5.0/bits/stl_pair.h:108:26: note: candidate expects 0 arguments, 2 provided
Compiler returned: 1
Use std::make_shared:
foo.emplace(0, std::make_shared<std::vector<uint8_t>>(1));
live example on wandbox.org
In C++11 the emplace method for map and unordered_map was cumbersome. By default it actually needs to be fed the construction parameters for the std::pair used for the insert method.
In c++17, I think, this is improved, so you can provide 2 arguments, and the second argument is only applied if the object is actually emplaced.
Meanwhile, back in C++11 you are forced to use the piecewise mechanism if you really want to insert objects on demand. C++11 emplace example
However, in your case, my main concern is that the new object is already built and might leak if it already has an index in the map, and does not get inserted. Using make_shared helps avoid that issue, at least!
I am trying to use push_back on a vector of strings in C++. How can I push a single character on to the vector? Currently, I have tried the following, all without success:
Initialized a string (tried to) with the character.
Code
string str(main_string[0]);
vector_string.push_back(str);
Tried to invoke strcpy and thus copy contents. Const-ness seems to get in the way.
Code
string str;
strcpy(main_string[0], str.c_str());
vector_string.push_back(str);
Any more suggestions/ideas are most welcome.
Edit: The error logs are as follows:
test_push.C: In function ‘void test_push(std::vector<std::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::allocator<std::basic_string<char, std::char_traits<char>, std::allocator<char> > > >&, std::string)’:
test_push.C:50: error: no matching function for call to ‘std::basic_string<char, std::char_traits<char>, std::allocator<char> >::basic_string(char&)’
/usr/lib/gcc/i586-redhat-linux/4.4.1/../../../../include/c++/4.4.1/bits/basic_string.tcc:220: note: candidates are: std::basic_string<_CharT, _Traits, _Alloc>::basic_string(typename _Alloc::rebind<_CharT>::other::size_type, _CharT, const _Alloc&) [with _CharT = char, _Traits = std::char_traits<char>, _Alloc = std::allocator<char>]
/usr/lib/gcc/i586-redhat-linux/4.4.1/../../../../include/c++/4.4.1/bits/basic_string.tcc:213: note: std::basic_string<_CharT, _Traits, _Alloc>::basic_string(const _CharT*, const _Alloc&) [with _CharT = char, _Traits = std::char_traits<char>, _Alloc = std::allocator<char>] <near match>
/usr/lib/gcc/i586-redhat-linux/4.4.1/../../../../include/c++/4.4.1/bits/basic_string.tcc:206: note: std::basic_string<_CharT, _Traits, _Alloc>::basic_string(const _CharT*, typename _Alloc::rebind<_CharT>::other::size_type, const _Alloc&) [with _CharT = char, _Traits = std::char_traits<char>, _Alloc = std::allocator<char>]
/usr/lib/gcc/i586-redhat-linux/4.4.1/../../../../include/c++/4.4.1/bits/basic_string.tcc:194: note: std::basic_string<_CharT, _Traits, _Alloc>::basic_string(const std::basic_string<_CharT, _Traits, _Alloc>&, typename _Alloc::rebind<_CharT>::other::size_type, typename _Alloc::rebind<_CharT>::other::size_type, const _Alloc&) [with _CharT = char, _Traits = std::char_traits<char>, _Alloc = std::allocator<char>]
/usr/lib/gcc/i586-redhat-linux/4.4.1/../../../../include/c++/4.4.1/bits/basic_string.tcc:184: note: std::basic_string<_CharT, _Traits, _Alloc>::basic_string(const std::basic_string<_CharT, _Traits, _Alloc>&, typename _Alloc::rebind<_CharT>::other::size_type, typename _Alloc::rebind<_CharT>::other::size_type) [with _CharT = char, _Traits = std::char_traits<char>, _Alloc = std::allocator<char>]
/usr/lib/gcc/i586-redhat-linux/4.4.1/../../../../include/c++/4.4.1/bits/basic_string.tcc:170: note: std::basic_string<_CharT, _Traits, _Alloc>::basic_string(const std::basic_string<_CharT, _Traits, _Alloc>&) [with _CharT = char, _Traits = std::char_traits<char>, _Alloc = std::allocator<char>]
/usr/lib/gcc/i586-redhat-linux/4.4.1/../../../../include/c++/4.4.1/bits/basic_string.tcc:178: note: std::basic_string<_CharT, _Traits, _Alloc>::basic_string(const _Alloc&) [with _CharT = char, _Traits = std::char_traits<char>, _Alloc = std::allocator<char>]
/usr/lib/gcc/i586-redhat-linux/4.4.1/../../../../include/c++/4.4.1/bits/basic_string.h:2147: note: std::basic_string<_CharT, _Traits, _Alloc>::basic_string() [with _CharT = char, _Traits = std::char_traits<char>, _Alloc = std::allocator<char>]
test_push.C:61: error: conversion from ‘char’ to non-scalar type ‘std::string’ requested
test_push:90: error: no matching function for call to ‘std::basic_string<char, std::char_traits<char>, std::allocator<char> >::basic_string(char&)’
/usr/lib/gcc/i586-redhat-linux/4.4.1/../../../../include/c++/4.4.1/bits/basic_string.tcc:220: note: candidates are: std::basic_string<_CharT, _Traits, _Alloc>::basic_string(typename _Alloc::rebind<_CharT>::other::size_type, _CharT, const _Alloc&) [with _CharT = char, _Traits = std::char_traits<char>, _Alloc = std::allocator<char>]
/usr/lib/gcc/i586-redhat-linux/4.4.1/../../../../include/c++/4.4.1/bits/basic_string.tcc:213: note: std::basic_string<_CharT, _Traits, _Alloc>::basic_string(const _CharT*, const _Alloc&) [with _CharT = char, _Traits = std::char_traits<char>, _Alloc = std::allocator<char>] <near match>
/usr/lib/gcc/i586-redhat-linux/4.4.1/../../../../include/c++/4.4.1/bits/basic_string.tcc:206: note: std::basic_string<_CharT, _Traits, _Alloc>::basic_string(const _CharT*, typename _Alloc::rebind<_CharT>::other::size_type, const _Alloc&) [with _CharT = char, _Traits = std::char_traits<char>, _Alloc = std::allocator<char>]
More info: In this function, either the entire string or just the first letter is pushed in to the vector based on its contents. So, in a way, I am looking for some function that converts a char into a C++ style std::string (to make it acceptable to a vector).
Thanks,
Sriram
You can also initialize with one character like this.
string str(1,main_string[0]);
vector_string.push_back(str);
You can add the character directly via an appropriate string constructor:
vector_string.push_back(string(1, main_string[0]));
EDIT: Removed alternate solution after realising it duplicates #Tim's.
no need for strcpy for a single character.
string str ("x");
str[0] = main_string[0];
vector_string.push_back(str);
This is a noob question sorry :(
I dont know why this gives me an error..could somebody please explain?
for example i wanna do:
vector<double> a;
string s("0.11 11.0002");
copys(s, a);
template <typename T, typename S>
void copys(T& c, S& d)
{
istringstream iss(c);
copy(istream_iterator<S>(iss), istream_iterator<S>(),
back_inserter(d));
return;
}
The error from the compiler is:
/usr/lib/gcc/i686-pc-cygwin/4.3.4/include/c++/bits/stream_iterator.h: In member function `void std::istream_iterator<_Tp, _CharT, _Trai
ts, _Dist>::_M_read() [with _Tp = std::vector<double, std::allocator<double> >, _CharT = char, _Traits = std::char_traits<char>, _Dist
= int]':
/usr/lib/gcc/i686-pc-cygwin/4.3.4/include/c++/bits/stream_iterator.h:68: instantiated from `std::istream_iterator<_Tp, _CharT, _Trait
s, _Dist>::istream_iterator(std::basic_istream<_CharT, _Traits>&) [with _Tp = std::vector<double, std::allocator<double> >, _CharT = ch
ar, _Traits = std::char_traits<char>, _Dist = int]'
ProfitCalculator.cpp:20: instantiated from `void copys(T&, S&) [with T = std::basic_string<char, std::char_traits<char>, std::allocat
or<char> >, S = std::vector<double, std::allocator<double> >]'
ProfitCalculator.cpp:41: instantiated from here
/usr/lib/gcc/i686-pc-cygwin/4.3.4/include/c++/bits/stream_iterator.h:119: error: no match for `operator>>' in `*((std::istream_iterator
<std::vector<double, std::allocator<double> >, char, std::char_traits<char>, int>*)this)->std::istream_iterator<std::vector<double, std
::allocator<double> >, char, std::char_traits<char>, int>::_M_stream >> ((std::istream_iterator<std::vector<double, std::allocator<doub
le> >, char, std::char_traits<char>, int>*)this)->std::istream_iterator<std::vector<double, std::allocator<double> >, char, std::char_t
raits<char>, int>::_M_value'
/usr/lib/gcc/i686-pc-cygwin/4.3.4/include/c++/istream:123: note: candidates are: std::basic_istream<_CharT, _Traits>& std::basic_istrea
m<_CharT, _Traits>::operator>>(std::basic_istream<_CharT, _Traits>& (*)(std::basic_istream<_CharT, _Traits>&)) [with _CharT = char, _Tr
aits = std::char_traits<char>]
/usr/lib/gcc/i686-pc-cygwin/4.3.4/include/c++/istream:127: note: std::basic_istream<_CharT, _Traits>& std::basic_istrea
m<_CharT, _Traits>::operator>>(std::basic_ios<_CharT, _Traits>& (*)(std::basic_ios<_CharT, _Traits>&)) [with _CharT = char, _Traits = s
td::char_traits<char>]
/usr/lib/gcc/i686-pc-cygwin/4.3.4/include/c++/istream:134: note: std::basic_istream<_CharT, _Traits>& std::basic_istrea
m<_CharT, _Traits>::operator>>(std::ios_base& (*)(std::ios_base&)) [with _CharT = char, _Traits = std::char_traits<char>]
/usr/lib/gcc/i686-pc-cygwin/4.3.4/include/c++/istream:170: note: std::basic_istream<_CharT, _Traits>& std::basic_istrea
m<_CharT, _Traits>::operator>>(bool&) [with _CharT = char, _Traits = std::char_traits<char>]
/usr/lib/gcc/i686-pc-cygwin/4.3.4/include/c++/istream:174: note: std::basic_istream<_CharT, _Traits>& std::basic_istrea
m<_CharT, _Traits>::operator>>(short int&) [with _CharT = char, _Traits = std::char_traits<char>]
/usr/lib/gcc/i686-pc-cygwin/4.3.4/include/c++/istream:177: note: std::basic_istream<_CharT, _Traits>& std::basic_istrea
m<_CharT, _Traits>::operator>>(short unsigned int&) [with _CharT = char, _Traits = std::char_traits<char>]
/usr/lib/gcc/i686-pc-cygwin/4.3.4/include/c++/istream:181: note: std::basic_istream<_CharT, _Traits>& std::basic_istrea
m<_CharT, _Traits>::operator>>(int&) [with _CharT = char, _Traits = std::char_traits<char>]
/usr/lib/gcc/i686-pc-cygwin/4.3.4/include/c++/istream:184: note: std::basic_istream<_CharT, _Traits>& std::basic_istrea
m<_CharT, _Traits>::operator>>(unsigned int&) [with _CharT = char, _Traits = std::char_traits<char>]
/usr/lib/gcc/i686-pc-cygwin/4.3.4/include/c++/istream:188: note: std::basic_istream<_CharT, _Traits>& std::basic_istrea
m<_CharT, _Traits>::operator>>(long int&) [with _CharT = char, _Traits = std::char_traits<char>]
/usr/lib/gcc/i686-pc-cygwin/4.3.4/include/c++/istream:192: note: std::basic_istream<_CharT, _Traits>& std::basic_istrea
m<_CharT, _Traits>::operator>>(long unsigned int&) [with _CharT = char, _Traits = std::char_traits<char>]
/usr/lib/gcc/i686-pc-cygwin/4.3.4/include/c++/istream:197: note: std::basic_istream<_CharT, _Traits>& std::basic_istrea
m<_CharT, _Traits>::operator>>(long long int&) [with _CharT = char, _Traits = std::char_traits<char>]
/usr/lib/gcc/i686-pc-cygwin/4.3.4/include/c++/istream:201: note: std::basic_istream<_CharT, _Traits>& std::basic_istrea
m<_CharT, _Traits>::operator>>(long long unsigned int&) [with _CharT = char, _Traits = std::char_traits<char>]
/usr/lib/gcc/i686-pc-cygwin/4.3.4/include/c++/istream:206: note: std::basic_istream<_CharT, _Traits>& std::basic_istrea
m<_CharT, _Traits>::operator>>(float&) [with _CharT = char, _Traits = std::char_traits<char>]
/usr/lib/gcc/i686-pc-cygwin/4.3.4/include/c++/istream:210: note: std::basic_istream<_CharT, _Traits>& std::basic_istrea
m<_CharT, _Traits>::operator>>(double&) [with _CharT = char, _Traits = std::char_traits<char>]
/usr/lib/gcc/i686-pc-cygwin/4.3.4/include/c++/istream:214: note: std::basic_istream<_CharT, _Traits>& std::basic_istrea
m<_CharT, _Traits>::operator>>(long double&) [with _CharT = char, _Traits = std::char_traits<char>]
/usr/lib/gcc/i686-pc-cygwin/4.3.4/include/c++/istream:218: note: std::basic_istream<_CharT, _Traits>& std::basic_istrea
m<_CharT, _Traits>::operator>>(void*&) [with _CharT = char, _Traits = std::char_traits<char>]
/usr/lib/gcc/i686-pc-cygwin/4.3.4/include/c++/istream:242: note: std::basic_istream<_CharT, _Traits>& std::basic_istrea
m<_CharT, _Traits>::operator>>(std::basic_streambuf<_CharT, _Traits>*) [with _CharT = char, _Traits = std::char_traits<char>]
/usr/lib/gcc/i686-pc-cygwin/4.3.4/include/c++/bits/stl_algobase.h: In static member function `static _OI std::__copy_move<<anonymous>,
<anonymous>, <template-parameter-1-3> >::__copy_m(_II, _II, _OI) [with _II = std::istream_iterator<std::vector<double, std::allocator<d
ouble> >, char, std::char_traits<char>, int>, _OI = std::back_insert_iterator<std::vector<double, std::allocator<double> > >, bool <ano
nymous> = false, bool <anonymous> = false, <template-parameter-1-3> = std::input_iterator_tag]':
/usr/lib/gcc/i686-pc-cygwin/4.3.4/include/c++/bits/stl_algobase.h:396: instantiated from `_OI std::__copy_move_a(_II, _II, _OI) [with
bool _IsMove = false, _II = std::istream_iterator<std::vector<double, std::allocator<double> >, char, std::char_traits<char>, int>, _O
I = std::back_insert_iterator<std::vector<double, std::allocator<double> > >]'
/usr/lib/gcc/i686-pc-cygwin/4.3.4/include/c++/bits/stl_algobase.h:435: instantiated from `_OI std::__copy_move_a2(_II, _II, _OI) [wit
h bool _IsMove = false, _II = std::istream_iterator<std::vector<double, std::allocator<double> >, char, std::char_traits<char>, int>, _
OI = std::back_insert_iterator<std::vector<double, std::allocator<double> > >]'
/usr/lib/gcc/i686-pc-cygwin/4.3.4/include/c++/bits/stl_algobase.h:466: instantiated from `_OI std::copy(_II, _II, _OI) [with _II = st
d::istream_iterator<std::vector<double, std::allocator<double> >, char, std::char_traits<char>, int>, _OI = std::back_insert_iterator<s
td::vector<double, std::allocator<double> > >]'
ProfitCalculator.cpp:20: instantiated from `void copys(T&, S&) [with T = std::basic_string<char, std::char_traits<char>, std::allocat
or<char> >, S = std::vector<double, std::allocator<double> >]'
ProfitCalculator.cpp:41: instantiated from here
/usr/lib/gcc/i686-pc-cygwin/4.3.4/include/c++/bits/stl_algobase.h:312: error: no match for `operator=' in `__result.std::back_insert_it
erator<_Container>::operator* [with _Container = std::vector<double, std::allocator<double> >]() = __first.std::istream_iterator<_Tp, _
CharT, _Traits, _Dist>::operator* [with _Tp = std::vector<double, std::allocator<double> >, _CharT = char, _Traits = std::char_traits<c
har>, _Dist = int]()'
/usr/lib/gcc/i686-pc-cygwin/4.3.4/include/c++/bits/stl_iterator.h:412: note: candidates are: std::back_insert_iterator<_Container>& std
::back_insert_iterator<_Container>::operator=(typename _Container::const_reference) [with _Container = std::vector<double, std::allocat
or<double> >]
/usr/lib/gcc/i686-pc-cygwin/4.3.4/include/c++/bits/stl_iterator.h:388: note: std::back_insert_iterator<std::vector<doub
le, std::allocator<double> > >& std::back_insert_iterator<std::vector<double, std::allocator<double> > >::operator=(const std::back_ins
ert_iterator<std::vector<double, std::allocator<double> > >&)
make: *** [ProfitCalculator] Error 1
The problem is that after template substitution, this
copy(istream_iterator<S>(iss), istream_iterator<S>(),
back_inserter(d));
becomes
copy(istream_iterator<vector<double> >(iss), istream_iterator<vector<double> >(),
^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^
back_inserter(d));
You don't want to extract vector<double>-s from the string (and stringstreams can't do that anyway). Instead you want to extract double-s from the string, and therefore you need the value_type of the vector (double in this case).
typedef typename S::value_type value_type;
copy(istream_iterator<value_type>(iss), istream_iterator<value_type>(),
back_inserter(d));
It's hard to say for sure without the error, but I see two potential problems:
Your template definition comes after its use, so the compiler doesn't know what copys(...) is supposed to be.
a vector<double> can't .push_back(std::string), and there's nothing in copys that I can see what would convert from a string representation to a double.
Update:
Alright, given the error in your question, I'd say the answer is #2.