473,809 Members | 2,660 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Iterator Implementation Compile error

9 New Member
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 1583
gpraghuram
1,275 Recognized Expert Top Contributor
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 New Member
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 Recognized Expert Moderator Expert
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
1619
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(), m_elements.end(), s); // snip }
5
2419
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
1301
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 -> ++ == != Forward =*p -> *p= ++ == != Bidirectional =*p -> *p= ++ -- == != Random Access =*p -> *p= ++ -- == != + - += -= < > <= >= I have an iterator-like class for which I would like to ensure
13
2546
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;} }; int main() { int arr = {1, 2, 3}; set<int,Asc> asc(arr, arr+3); set<int,Des>::iterator beg = asc.begin(); // set<int,Des>::iterator end = asc.end(); // copy(beg, end, ostream_iterator<int>(cout,...
1
6455
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 operator++, etc., and begin(), end(), and it works as a drop in for std::map if you stick to the methods I have implemented. Anyway, I decided to go and add const_iterator support, thinking that it would be simple, but I ran into a confusing...
21
5727
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 classes by inheriting from it, ie. class my_bidirectional_iterator : public bidirectional_iterator<double> { ... };
6
8622
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 a messy error. Here are what I try to do: typedef unordered_map<string, unsignedStringHash;
22
1580
by: =?gb2312?B?wNbA1rTzzOzKpg==?= | last post by:
windows xp, visual studio 2005 ---------------------------------------------------------------------------------------------------------------------------------- #include <iostream> #include <map> using namespace std; int main() { map<int, int>::iterator it = 0; if( it != 0 ) //break point,
16
6088
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 element in the container? Or is there a cleaner way of getting an iterator to the last element?
0
9721
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9603
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10120
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9200
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7662
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6881
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5550
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5689
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4332
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.