No matching constructor for initalization of 'ostream_iterator'

前端 未结 3 1027
清歌不尽
清歌不尽 2020-12-19 13:33

for the code, why error, osteam_iterator is a template class ,why no matching constructor for initalization of \'ostream_iterator\', please give a help , thank you. define o

3条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-12-19 14:19

    The ostream_iterator class definition looks like:

    template< class T,
      class CharT = char,
      class Traits = std::char_traits>
    class ostream_iterator /*...*/
    

    whereas the respective constructor is declared as:

    ostream_iterator(ostream_type& buffer, const CharT* delim)
    

    Since the second template argument of an ostream_iterator is required to be of character type you cannot simply replace it with int.

    If you ommit the second template parameter you can plug in a string literal of type char const *:

    std::copy(sentence1.begin(), sentence1.end(), std::ostream_iterator(cout, ","));
    

    If C++11 is available to you then

    int c = 5;
    for ( auto v : sentence1 ) std::cout << v << c;
    

    is another way of doing what you deserve and it might be suitable, too. The advantage is, that operator<< is more flexible than an argument of type "pointer to char type".

提交回复
热议问题