| | |
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:
Solved Threads: 1
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
I replace the for loop with this line
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:
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 -
and my output operator doesnt?
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:
cpp Syntax (Toggle Plain Text)
template<typename T> class list{ class listNode{ public: listNode* prev; listNode* next; T data; listNode(listNode* newp=NULL, listNode* newn=NULL, const T& newd=T()) :prev(newp), next(newn), data(newd) {} }; listNode* head; listNode* tail; int s; public: class iterator{ friend class list; listNode* ptr; public: iterator(listNode& lnr) : ptr(&lnr) {} iterator(listNode* p=NULL) : ptr(p) {} iterator& operator++() { ptr=ptr->next; return *this; }//pre iterator operator++(int a) { iterator retval = *this; ++*this; return retval; }//post iterator& operator--() { ptr=ptr->prev; return *this; }//pre iterator operator--(int a) { iterator retval = *this; --*this; return retval; }//post T& operator*() const { return ptr->data; } bool operator==(const iterator& rhs) const { return ptr == rhs.ptr; } bool operator!=(const iterator& rhs) const { return ptr!=rhs.ptr; } }; iterator begin() const { return iterator(head->next); } iterator end() const { return iterator(tail); } }; template<typename U> std::ostream& operator<<(std::ostream& out, const list<U>& lt) { list<U>::iterator lti; //<----this line gives error, thinks semicolon is missing //for(lti=lt.begin(); lti!=lt.end(); ++lti){ // out << *lti << ' '; //} //out << std::endl; }
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 -
cpp Syntax (Toggle Plain Text)
int main(int argc, char** argv) { list<int> listOfInt; for(int i=0; i<10; ++i) listOfInt.push_back(i); for(list<int>::iterator iter=listOfInt.begin(); iter!=listOfInt.end(); ++iter) std::cout << *iter << ' '; return (EXIT_SUCCESS); }
and my output operator doesnt?
Last edited by Zcool31; Apr 4th, 2009 at 10:20 pm.
class NoClass {
~NoClass () { delete this; }
}; "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.
All my posts may be freely redistributed under the terms of the MIT license.
![]() |
Other Threads in the C++ Forum
- Previous Thread: Matrix Multiplication using Multi-Dimensional Arrays
- Next Thread: need help on my inheritance here
| Thread Tools | Search this Thread |
api array arrays based beginner binary bitmap c++ c/c++ calculator char char* class classes coding compile compiler console conversion convert count data database delete desktop developer directshow dll dynamiccharacterarray email encryption error file forms fstream function functions game generator getline google graph homeworkhelper iamthwee ifstream input int integer java lib linkedlist linux list loop looping loops map math matrix memory multiple news node number numbertoword output parameter pointer problem program programming project proxy python random read recursion recursive reference return rpg sorting string strings struct template templates text tree unix url vector video visualstudio win32 windows winsock word wordfrequency wxwidgets






