file i/o with stream_iterators - error

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Aman

    file i/o with stream_iterators - error

    Hi,
    could you please tell me why the following program gives a compilation
    error ? what does this error mean ?
    note also that when i derefernce the iterator , the contents are
    displayed.

    int main()
    {
    ifstream ifs("file") ;
    istream_iterato r<string> i_it(ifs) ;
    copy(i_it.begin (), i_it.end(),ostr eam_iterator<st ring>(cout,"\n" )) ;
    }
    _______________ ____________
    Complier error -
    (SunOS 5.8 , g++ )
    13 : no matching function for call to `istream_iterat or<basic_stri
    ng<char,string_ char_traits<cha r>,__default_al loc_template<fa lse,0>[color=blue]
    >,int>::begin ()'[/color]
    13: no matching function for call to `istream_iterat or<basic_stri
    ng<char,string_ char_traits<cha r>,__default_al loc_template<fa lse,0>[color=blue]
    >,int>::end ()'[/color]
    _______________ _______
    regards,


    --
    Posted via Mailgate.ORG Server - http://www.Mailgate.ORG
  • Rob Williscroft

    #2
    Re: file i/o with stream_iterator s - error

    Aman wrote in news:450f74dbb8 5ab55d80d1df17a a0dd289.26421
    @mygate.mailgat e.org:
    [color=blue]
    > Hi,
    > could you please tell me why the following program gives a compilation
    > error ? what does this error mean ?
    > note also that when i derefernce the iterator , the contents are
    > displayed.
    >
    > int main()
    > {
    > ifstream ifs("file") ;
    > istream_iterato r<string> i_it(ifs) ;
    > copy(i_it.begin (), i_it.end(),ostr eam_iterator<st ring>(cout,"\n" )) ;[/color]


    copy(
    i_it,
    istream_iterato r<string>(),
    ostream_iterato r<string>(cout, "\n")
    );

    [color=blue]
    > }
    > _______________ ____________
    > Complier error -
    > (SunOS 5.8 , g++ )
    > 13 : no matching function for call to `istream_iterat or<basic_stri
    > ng<char,string_ char_traits<cha r>,__default_al loc_template<fa lse,0>[color=green]
    >>,int>::begi n ()'[/color][/color]

    Your trying to use an input iterator like its a container.

    As I've done above use a default constructed istream_iterato r<string>
    as the end iterator.

    [color=blue]
    > 13: no matching function for call to `istream_iterat or<basic_stri
    > ng<char,string_ char_traits<cha r>,__default_al loc_template<fa lse,0>[color=green]
    >>,int>::end ()'[/color]
    > _______________ _______[/color]


    HTH

    Rob.
    --

    Comment

    • Aman Angrish

      #3
      Re: file i/o with stream_iterator s - error

      [color=blue]
      >
      > copy(
      > i_it,
      > istream_iterato r<string>(),
      > ostream_iterato r<string>(cout, "\n")
      > );
      >[/color]

      That's neat .
      [color=blue]
      >
      > HTH
      >
      > Rob.[/color]

      Thanks Rob !,

      regards,
      Aman.




      --
      Posted via Mailgate.ORG Server - http://www.Mailgate.ORG

      Comment

      • David Rubin

        #4
        Re: file i/o with stream_iterator s - error

        Rob Williscroft wrote:

        [snip][color=blue]
        > copy(
        > i_it,
        > istream_iterato r<string>(),
        > ostream_iterato r<string>(cout, "\n")
        > );[/color]

        Can you please explain why this works. I was under the impression that
        the 'end' iterator had to be related to the 'begin' iterator in some
        way. Thanks,

        /david

        --
        Andre, a simple peasant, had only one thing on his mind as he crept
        along the East wall: 'Andre, creep... Andre, creep... Andre, creep.'
        -- unknown

        Comment

        • Adam Fineman

          #5
          Re: file i/o with stream_iterator s - error

          David Rubin wrote:[color=blue]
          > Rob Williscroft wrote:
          >
          > [snip]
          >[color=green]
          >> copy(
          >> i_it,
          >> istream_iterato r<string>(),
          >> ostream_iterato r<string>(cout, "\n")
          >> );[/color]
          >
          >
          > Can you please explain why this works. I was under the impression that
          > the 'end' iterator had to be related to the 'begin' iterator in some
          > way. Thanks,
          >[/color]

          You snipped the declaration and definition of i_it, so here it is again:
          ------------------------------------------
          ifstream ifs("file") ;
          istream_iterato r<string> i_it(ifs) ;
          ------------------------------------------

          The 'end' iterator of any istream_iterato r<T> is istream_iterato r<T>().
          I'll agree that the syntax is not intuitive, as it looks like a call
          to the default constructor, but that's something we all have to live with.

          As another example, here is a program that fills a container from the
          standard input:

          -------------------------------------
          #include <vector>
          #include <string>
          #include <iterator>
          #include <iostream>

          using namespace std;

          int
          main()
          {
          vector<string> v;

          copy(istream_it erator<string>( cin),
          istream_iterato r<string>(), // 'end' iterator
          back_inserter(v ));

          cout << "Recieved " << v.size() << " strings as input.\n";

          return 0;
          }
          -------------------------------------

          --
          Reverse domain name to reply.

          Comment

          • Julián Albo

            #6
            Re: file i/o with stream_iterator s - error

            David Rubin escribió:
            [color=blue][color=green]
            > > copy(
            > > i_it,
            > > istream_iterato r<string>(),
            > > ostream_iterato r<string>(cout, "\n")
            > > );[/color]
            >
            > Can you please explain why this works. I was under the impression that
            > the 'end' iterator had to be related to the 'begin' iterator in some
            > way. Thanks,[/color]

            Because the operator = that compares two istream_iterato r is written
            that way, it gives a value of true when comparing one that has reached
            his end to one default constructed. It's a simple solution that does not
            need to define additional functions.

            Regards.

            Comment

            Working...