This is a noob question sorry :( I dont know why this gives me an error..could so开发者_运维知识库mebody 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 incopys
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.
精彩评论