Thread: Help with C# iterators

  1. #1
    Use this: dudeomanodude's Avatar
    Join Date
    Jan 2008
    Location
    Hampton, VA
    Posts
    391

    Help with C# iterators

    I'm trying to write a simple generic linked-list, for some reason I can't get the iterators to work for me.

    Here's my code:
    Code:
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    
    
    namespace ConsoleApplication1
    {
        public class GenericList<T> : IEnumerable<T>
        {
            private class Node
            {
                // Constructor
                public Node(T t)
                {
                    data = t;
                    next = null;
                }
    
                private T data;
                public T Data
                {
                    get { return data; }
                    set { data = value; }
                }
    
                private Node next;
                public Node Next
                {
                    get { return next; }
                    set { next = value; }
                }
    
            }
    
            private Node head;
    
            public GenericList()
            {
                head = null;
            }
    
            public void push_front(T t)
            {
                if (head != null)
                {
                    Node n = new Node(t);
                    n.Next = head;
                    head = n;
                }
    
                else
                {
                    head = new Node(t);
                }
            }
    
    
            /*  This is where I'm having trouble:
            */
            public IEnumerator<T> GetEnumerator()
            {
                Node curr = head;
                while (curr != null)
                {
                    yield return curr.Data;
                    curr = curr.Next;
                }
            }
    
            IEnumerator IEnumerable.GetEnumerator()
            {
                return GetEnumerator();
            }
    
            public void display()
            {
                Node curr = head;
                while (curr != null)
                {
                    Console.WriteLine(curr.Data);
                    curr = curr.Next;
                }
            }
    
        }
    
        class Program
        {
            static void Main(string[] args)
            {
                GenericList<int> mList = new GenericList<int>();
    
                for( int i = 0; i < 10; i++ )
                {
                    mList.push_front(i);
                }
    
                foreach (T currData in mList)
                {
                    Console.WriteLine(currData, "\n");
                }
            }
        }
    }
    Here's my error message from VS:
    Code:
    Error	2	Using the generic type 'System.Collections.Generic.IEnumerator<T>' requires '1' type arguments	C:\Documents and Settings\Owner\Local Settings\Application Data\Temporary Projects\ConsoleApplication1\Program.cs	68	9	ConsoleApplication1
    Thanks in advance


    EDIT: I caught one silly thing:
    Code:
    foreach( T currData in mList )
    
    // Changed it to:
    foreach( int currData in mList )
    but i still get the same error message.
    Last edited by dudeomanodude; 04-27-2008 at 10:14 PM.
    Ubuntu Desktop
    GCC/G++
    Geany (for quick projects)
    Anjuta (for larger things)

  2. #2
    Ethernal Noob
    Join Date
    Nov 2001
    Posts
    1,901
    Code:
             IEnumerator IEnumerable.GetEnumerator()
            {
                return GetEnumerator();
            }
    Tjat should return an IEnumerator<T>

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. vector of strings with iterators.
    By Mario F. in forum C++ Programming
    Replies: 6
    Last Post: 05-31-2006, 12:12 PM
  2. Using reverse iterators in algorithms
    By 0rion in forum C++ Programming
    Replies: 1
    Last Post: 02-27-2006, 03:19 AM
  3. Writing Iterators...
    By Geolingo in forum C++ Programming
    Replies: 6
    Last Post: 05-29-2003, 09:31 PM
  4. accessing vector elements: iterators are faster?
    By Captain Penguin in forum C++ Programming
    Replies: 1
    Last Post: 11-28-2002, 02:27 PM
  5. Generic Progpammimg Iterators
    By rip1968 in forum C++ Programming
    Replies: 7
    Last Post: 08-06-2002, 10:20 AM