STL, istream_iterator class doesn't compileq

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Oct 2006
Posts: 23
Reputation: jrivera is an unknown quantity at this point 
Solved Threads: 0
jrivera jrivera is offline Offline
Newbie Poster

STL, istream_iterator class doesn't compileq

 
0
  #1
May 7th, 2007
I need help trying to compile and test this code, but I keep getting the error: template is incomplete, can not parse field.

Here is my code:
  1. #include<iostream>
  2. #include<iterator>
  3. #include<vector>
  4. #include<algorithm>
  5. using namespace std;
  6.  
  7. template <class T>
  8. class my_istream_iterator
  9. {
  10. public:
  11. typedef charT char_type;
  12. typedef traitsT traits_type;
  13. typedef basic_istream<charT, traitsT> istream_type;
  14. typedef input_iterator_tag iterator_category;
  15. typedef T value_type;
  16. typedef diff difference_type;
  17. typedef const T* pointer;
  18. typedef const T& reference;
  19.  
  20.  
  21. my_istream_iterator():is(0), pred(false) {}
  22. my_istream_iterator( istream_type &s ):is(&s) { read_next();}
  23.  
  24. reference operator*() const
  25. {
  26. return current;
  27. }
  28. pointer operator->() const
  29. {
  30. return &(operator*());
  31. }
  32.  
  33. my_istream_iterator& operator++()
  34. {
  35. read_next();
  36. return *this;
  37. }
  38.  
  39. my_istream_iterator operator++(int)
  40. {
  41. my_istream_iterator temp = *this;
  42. read_next();
  43. return temp;
  44. }
  45.  
  46. bool my_equal(const my_istream_iterator &x) const
  47. {
  48. return (pred == x.pred) && (!pred || is == x.is);
  49. }
  50.  
  51. private:
  52. T current;
  53. istream_type *is;
  54. bool pred;
  55.  
  56. void read_next()
  57. {
  58. if( is == NULL || (is >> current))
  59. is = NULL;
  60.  
  61. }
  62. };
  63.  
  64. template<class T>
  65. bool operator==(const my_istream_iterator<T> &is1, const my_istream_iterator<T> &is2)
  66. {
  67. return is1.my_equal(is2);
  68. }
  69.  
  70. template<class T>
  71. bool operator!=(const my_istream_iterator<T> &is1, const my_istream_iterator<T> &is2)
  72. {
  73. return !is.my_equal(is2);
  74. }
  75.  
  76. main()
  77. {
  78. vector<int> v;
  79. my_istream_iterator<vector<int> > is(cin);
  80. my_istream_iterator<vector<int> > eof;
  81. copy(is, eof, back_inserter(v));
  82. copy(v.begin(), v.end(), ostream_iterator<int>(cout, "\t"));
  83. cout << endl;
  84. }


can someone tell me what I'm doing wrong and how do I make the template complete?

Thank You,
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,652
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 721
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: STL, istream_iterator class doesn't compileq

 
0
  #2
May 7th, 2007
Your types are all screwed up. You use charT, traitsT, and diff without defining them, for instance.
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC