Implementing Linked List iterator, gcc is acting wierd

Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Mar 2009
Posts: 33
Reputation: Zcool31 is an unknown quantity at this point 
Solved Threads: 1
Zcool31 Zcool31 is offline Offline
Light Poster

Implementing Linked List iterator, gcc is acting wierd

 
0
  #1
Apr 4th, 2009
I'm trying to implement a linked list class in a manner similar to STL. Everyithing seems to work fine, but when I try to implement an output operator to let me see what is inside the collection quickly, it starts acting wierd. Note that I use the iterator inside the linked list class in several methods, as well as in main to iterate through the list. However, when I try a simple generic output operator like
template<typename T>
std::ostream& operator<<(std::ostream& out, list<T> lt){
  for(list<T>::iterator i=lt.begin(); i!=lt.end(); ++i) //this line gives me errors
    out << *i << ' ';
  out << std::endl;
}

I replace the for loop with this line list<T>::iterator i; , and gcc gives me an error, saying that there is a semicolon missing before the i.

The exact same code in main (with all T replaced by int or double or whatever I was using) works flawlessly.

Here's my source:

  1. template<typename T>
  2. class list{
  3. class listNode{
  4. public:
  5. listNode* prev;
  6. listNode* next;
  7. T data;
  8. listNode(listNode* newp=NULL, listNode* newn=NULL, const T& newd=T()) :prev(newp), next(newn), data(newd) {}
  9. };
  10. listNode* head;
  11. listNode* tail;
  12. int s;
  13. public:
  14. class iterator{
  15. friend class list;
  16. listNode* ptr;
  17. public:
  18. iterator(listNode& lnr) : ptr(&lnr) {}
  19. iterator(listNode* p=NULL) : ptr(p) {}
  20. iterator& operator++() { ptr=ptr->next; return *this; }//pre
  21. iterator operator++(int a) { iterator retval = *this; ++*this; return retval; }//post
  22. iterator& operator--() { ptr=ptr->prev; return *this; }//pre
  23. iterator operator--(int a) { iterator retval = *this; --*this; return retval; }//post
  24. T& operator*() const { return ptr->data; }
  25. bool operator==(const iterator& rhs) const { return ptr == rhs.ptr; }
  26. bool operator!=(const iterator& rhs) const { return ptr!=rhs.ptr; }
  27. };
  28. iterator begin() const { return iterator(head->next); }
  29. iterator end() const { return iterator(tail); }
  30. };
  31.  
  32. template<typename U>
  33. std::ostream& operator<<(std::ostream& out, const list<U>& lt) {
  34. list<U>::iterator lti; //<----this line gives error, thinks semicolon is missing
  35. //for(lti=lt.begin(); lti!=lt.end(); ++lti){
  36. // out << *lti << ' ';
  37. //}
  38. //out << std::endl;
  39. }


Of course, I have all the typical methods you would expect from a list class, like push_front, push_back, pop_front, pop_back, insert(iterator), remove(iterator), but I didn't include them here because they all work perfectly.

So could anyone tell me why this works -
  1. int main(int argc, char** argv)
  2. {
  3. list<int> listOfInt;
  4. for(int i=0; i<10; ++i)
  5. listOfInt.push_back(i);
  6. for(list<int>::iterator iter=listOfInt.begin(); iter!=listOfInt.end(); ++iter)
  7. std::cout << *iter << ' ';
  8. return (EXIT_SUCCESS);
  9. }

and my output operator doesnt?
Last edited by Zcool31; Apr 4th, 2009 at 10:20 pm.
class NoClass {
	~NoClass () { delete this; }
};
Reply With Quote Quick reply to this message  
Join Date: Apr 2006
Posts: 5,051
Reputation: John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold 
Solved Threads: 332
Team Colleague
John A's Avatar
John A John A is offline Offline
Vampirical Lurker

Re: Implementing Linked List iterator, gcc is acting wierd

 
1
  #2
Apr 4th, 2009
"Technological progress is like an axe in the hands of a pathological criminal."

All my posts may be freely redistributed under the terms of the MIT license.
Reply With Quote Quick reply to this message  
Join Date: Mar 2009
Posts: 33
Reputation: Zcool31 is an unknown quantity at this point 
Solved Threads: 1
Zcool31 Zcool31 is offline Offline
Light Poster

Re: Implementing Linked List iterator, gcc is acting wierd

 
0
  #3
Apr 5th, 2009
I love you! Thank you so much! I never thought it would work like that! Just had to put typename list<T>::iterator instead of just list<T>::iterator .

Solved
class NoClass {
	~NoClass () { delete this; }
};
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
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