[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

Re: C++: istream_iterator<string> has no component named operator!=.



> Shaul Karl <shaulka@bezeqint.net> writes:
> 
> > What am I doing wrong?
> > Summary:
> > 
> > Breakpoint 1, main () at main.cc:15
> > 15          for (; iter1 != iter2; iter1++) cout << *iter1;
> > (gdb) p iter1 != iter2
> > Structure has no component named operator!=.
> > (gdb) 
> > 
> > Full details:
> > 
> > $ cat main.cc
> > #include <algorithm>
> > #include <fstream>
> > #include <iostream>
> > #include <iterator>
> > #include <string>
> > 
> > using namespace std;
> > 
> > int main() 
> > {
> >     ifstream input("main.cc");
> >     istream_iterator<string> iter1(input), iter2, eof;
> >     iter1 = find(iter1, iter2, "main()");
> >     iter2 = find(iter1, eof, "}");
>               ^^^^
> These are your problem.  If you put 
> 
>     cout << "iter1: " << *iter2 << "\titer2: " << *iter2 << "\n";
> 
> immediately following those find statements, you'll get the output
> 
>     iter1: }	iter2: }
> 
> which means the find statements aren't doing what you want.  The for
> loop is working fine, however.
> 
> I don't know much about istream_iterators, and I don't have any of my
> C++ books on me right now to look it up.  However, I'd guess the problem
> is in your initialization of iter2 and eof.  They'd need to be
> initialized to something that points to something in input after iter1
> for the find algorithm to work.
> 


You didn't get the expected printing of *iter1 and *iter2 because you 
have a typo. The printing line uses *iter2 where you wanted *iter1.
As for the initialization of iter2 and eof, as far as I know they 
assume the default value, which is EOF.
Beside, having it compiled without errors/warnings although gdb comment 
that there is no != method is odd.


> >     for (; iter1 != iter2; iter1++) cout << *iter1;
> >     cout << "\n";
> >     return 0;
> > }
> > 
> > $ g++-3.0 -Wall -ggdb -o main main.cc
> > $ $ ./main 
> > 
> > $ gdb main
> > (gdb) b 15
> > Breakpoint 1 at 0x804d6b8: file main.cc, line 15.
> > (gdb) r
> > Starting program: /tmp/main 
> > 
> > Breakpoint 1, main () at main.cc:15
> > 15          for (; iter1 != iter2; iter1++) cout << *iter1;
> > (gdb) p iter1 != iter2
> > Structure has no component named operator!=.
> > (gdb) 
> > 
> > 
> > Debian version is testing.
> 
> -- 
> Brian Nelson <nelson@bignachos.com>
> BigNachos@jabber.org
> http://bignachos.com
> 



-- 

    Shaul Karl
    email: shaulka(at-no-spam)bezeqint.net 
           Please replace (at-no-spam) with an at - @ - character.
           (at-no-spam) is meant for unsolicitate mail senders only.




Reply to: