473,405 Members | 2,261 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

Iterator Implementation Compile error

9
Hi,

I am trying to implement an iterator to a List class. The code is included below. I get the following compiler error. At the point where I use a class that is defined inside a template (nList<T>::Link). The Link class is defined inside of a template. (I assume that is possible?). Any one know what I doing wrong?

Many Thanks,
Purush

Error from Compiler (gcc 3.4.6)
nList.cc:52: error: expected `;' before "current_"
*** Error code 1

Stop in /usr/home/annu/work/c++/Containers/dlists.



Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2.  
  3. template<class T> class Iterator
  4. {
  5.     public:
  6.         virtual T* first() = 0;
  7.         virtual T* next() = 0;
  8.         virtual T* previous() = 0;
  9. };
  10.  
  11. template<class T> class List_Iterator;
  12.  
  13. template<class T> class nList
  14. {
  15.     public:
  16.         nList(): head_(0), tail_(0), size_(0) {}
  17.         void push_front(const T& r);
  18.         void push_back(const T& r);
  19.         void insert(int position, const T r);
  20.         int size() const;
  21.         void print() const;
  22.         class Bad_Range{};
  23.         //friend class List_Iterator<T>;
  24.  
  25.         class Link
  26.         {
  27.             public:
  28.             Link* next;
  29.             Link* previous;
  30.             T val;
  31.             Link(T v):next(0), previous(0),val(v) {}
  32.             Link():next(0), previous(0) {}
  33.         };
  34.  
  35.  
  36.     private:
  37.         Link* head_;
  38.         Link* tail_;
  39.         int size_;
  40. };
  41.  
  42.  
  43. template<class T> class List_Iterator : public Iterator<T>
  44. {
  45.     public:
  46.         T* first();
  47.         T* next();
  48.         T* previous();
  49.         List_Iterator(nList<T>& l): list_(l){};
  50.     private:
  51.         nList<T>& list_;
  52.         nList<T>::Link current_;
  53. };
  54.  
  55. //template<class T> T* List_Iterator<T>::first()
  56. //{
  57. //    return &list_.front();
  58. //}
  59. //
  60. //template<class T> T* List_Iterator<T>::next()
  61. //{
  62. //    if (current_)
  63. //        current_ = current_->next;
  64. //
  65. //    return &current_->val;
  66. //}
  67.  
  68. //template<class T> T* List_Iterator<T>::previous()
  69. //{
  70. //    if (current_)
  71. //        current_ = current_->previous;
  72. //
  73. //    return &current_->val;
  74. //}
  75.  
  76. template<class T> int nList<T>::size() const
  77. {
  78.     return size_;
  79. }
  80.  
  81. template<class T> void nList<T>::print() const
  82. {
  83.     Link* l;
  84.  
  85.     for (l = head_; l; l = l->next)
  86.         std::cout<<l->val<<'\n';
  87.  
  88. }
  89.  
  90. template<class T> void nList<T>::push_front(const T& r)
  91. {
  92.     Link* l = new Link(r);
  93.  
  94.     l->next = head_;
  95.     l->previous = 0;
  96.  
  97.     if (head_)
  98.         head_->previous = l;
  99.  
  100.     head_ = l;
  101.  
  102.     if (!tail_)
  103.         tail_ = l;
  104.  
  105.     size_++;
  106. }
  107.  
  108. template<class T> void nList<T>::push_back(const T& r)
  109. {
  110.     Link* l = new Link(r);
  111.  
  112.     l->previous = tail_;
  113.     l->next = 0;
  114.  
  115.     if (tail_)
  116.         tail_->next = l;
  117.  
  118.     tail_ = l;
  119.  
  120.     if (!head_)
  121.         head_ = l;
  122.  
  123.     size_++;
  124. }
  125.  
  126. template<class T> void nList<T>::insert(int position, const T r)
  127. {
  128.  
  129.     if (position > size_)
  130.         throw Bad_Range();
  131.  
  132.     Link* l = new Link(r);
  133.  
  134.     Link* iterator = head_;
  135.     for (int i = 0; i < position; i++)
  136.         iterator = iterator->next ;
  137.  
  138.     if (iterator)
  139.     {
  140.         iterator->previous->next = l;
  141.         l->next = iterator;
  142.         iterator->previous = l;
  143.     }
  144.     else
  145.     {
  146.         push_back(r);
  147.     }
  148.     size_++;
  149. }
  150.  
  151. int main()
  152. {
  153.     nList<int> il;
  154. }
  155.  
Apr 25 '08 #1
3 1562
gpraghuram
1,275 Expert 1GB
I havent gone through the code.
But i tried compiling in my G++(cygwin) version 2.95.3-4 and it compiled successfully


raghuram
Apr 25 '08 #2
pnayak
9
I tried on another box with a 4.2 version of gcc and same problem. However the Error message in this case is,

nList.cc:50: error: type 'nList<T>' is not derived from type 'List_Iterator<T>'
nList.cc:50: error: expected ';' before 'current_'
Apr 25 '08 #3
weaknessforcats
9,208 Expert Mod 8TB
nList<T>::Link current_;
is a thing called a dependent type. Probably the class should look like:
Expand|Select|Wrap|Line Numbers
  1. template<class T> class List_Iterator : public Iterator<T>
  2. {
  3.     public:
  4.         T* first();
  5.         T* next();
  6.         T* previous();
  7.         List_Iterator(nList<typename T>& l): list_(l){};
  8.     private:
  9.         nList& list_;
  10.         nList::Link current_;
  11. };
  12.  
Visual Studio.NET 2008 gets the same error you get but that error goes away when the class is changed as above.
Apr 25 '08 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: William Payne | last post by:
Consider this (templated) class member function: template<typename Type> void CircularContainer<Type>::insert(const Type& s) { vector<Type>::iterator itr = find(m_elements.begin(),...
5
by: Johann Gerell | last post by:
I have this iterator: class CFileIterator : public std::iterator<std::input_iterator_tag, const std::wstring> { public: CFileIterator(const std::wstring& wildcardPath); .... };
1
by: Steve H | last post by:
From "C++ Programming Language (3rd Edition)" I understand that there are 5 standard iterator categories defined in the "std" namespace: Output *p= p++ Input =*p -> ++ == !=...
13
by: Dan Tsafrir | last post by:
is the following code standard? (cleanly compiles under g++-4.0.2): struct Asc { bool operator()(int a, int b) {return a < b;} }; struct Des { bool operator()(int a, int b) {return b > a;} };...
1
by: flopbucket | last post by:
Hi, For the learning experience, I am building a replacement for std::map. I built a templated red-black tree, etc., and all the basic stuff is working well. I implemented basic iterators and...
21
by: T.A. | last post by:
I understand why it is not safe to inherit from STL containers, but I have found (in SGI STL documentation) that for example bidirectional_iterator class can be used to create your own iterator...
6
by: Rares Vernica | last post by:
Hi, I am using tr1/unordered_map in my code. My code compiled ok on g++ (GCC) 4.1.2 (Ubuntu 4.1.2-0ubuntu1). Now I need to compile my code on g++ (GCC) 4.0.3 (Ubuntu 4.0.3-1ubuntu5). I get...
22
by: =?gb2312?B?wNbA1rTzzOzKpg==?= | last post by:
windows xp, visual studio 2005 ---------------------------------------------------------------------------------------------------------------------------------- #include <iostream> #include <map>...
16
by: Juha Nieminen | last post by:
I'm actually not sure about this one: Does the standard guarantee that if there's at least one element in the data container, then "--container.end()" will work and give an iterator to the last...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.