定义模板c ++

时间:2011-02-11 16:06:40

标签: c++ templates

这是一个noob问题抱歉:( 我不知道为什么这会给我一个错误..有人可以解释一下吗? 例如,我想做:

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;
}

编译器的错误是:

   /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

2 个答案:

答案 0 :(得分:5)

问题是模板替换后,这个

copy(istream_iterator<S>(iss), istream_iterator<S>(),
        back_inserter(d));

变为

copy(istream_iterator<vector<double> >(iss), istream_iterator<vector<double> >(),
                      ^^^^^^^^^^^^^^^                         ^^^^^^^^^^^^^^^
        back_inserter(d));

您不想从字符串中提取vector<double> - s(并且字符串流无论如何都不能这样做)。相反,您想从字符串中提取double - s,因此您需要向量的value_type(在这种情况下为double)。

typedef typename S::value_type value_type;
copy(istream_iterator<value_type>(iss), istream_iterator<value_type>(),
        back_inserter(d));

答案 1 :(得分:0)

如果没有错误,很难肯定地说,但我发现了两个潜在的问题:

  1. 您的模板定义在使用之后出现,因此编译器不知道copys(...)应该是什么。
  2. vector<double>无法.push_back(std::string)copys中没有任何内容可以查看从字符串表示转换为双精度内容的内容。
  3. <强>更新: 好吧,鉴于你的问题中的错误,我会说答案是#2。