[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.

>     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



Reply to: