CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2
  1. #1
    Join Date
    Nov 2014
    Posts
    1

    Trouble getting C++ program to print list using Iterator and Operator Overloading

    Hello All, First time here. I was hoping you could help me with my program. I'm trying to use the given Iterators to overload my = operator to print my list to screen. I keep getting Link2019 errer though and have narrowed the problem down to my print operator. I'm wondering if it has anything to do with the fact that my operator is in private part of class? I'm new to this concept.

    Code:
    #include    "List.h"
    
    // methods for Node all 
    //ME
    
    Node::Node( const string &s, Node * p, Node * z) : word( s ), next( p ), prev(z)		//constructor
    {
        word = s;               // init. word data with a copy of s
        next = p;               // next pointer points to p
    	prev = z;				//prev pointer points to z
    }
    
    const string &Node::get_word( ) const	// get a const reference to word
    {
        return word;						//returns curent objects data
    }
     
    Node *Node::get_next( ) const	// get a Node * (value of next)
    {
    	return next;				//returns current objects pointer to next
    }
    
                                
    Node *Node::set_next( Node * p )	// set next to a new value
    {
        next = p;               // next points to p
        return next;
    }
    
    Node *Node::get_prev( ) const	// get a Node * (value of next)
    {
    	return prev;				//returns current objects pointer to next
    }
    
    Node *Node::set_prev( Node * z )	// get a Node * (value of next)
    {
    	prev = z;
    	return prev;				//returns current objects pointer to next
    }
    
    
    
    
    //****************************************************************************8
    // methods for List
    
    
    List::List( )               // constructor: init. head and tail
    {
        cout << "List::List( )\n";
    
        head = tail = 0;
    }
    
    
    List::List( const List &rhs )	//Copy Constructor
    {
    	copy_list( rhs );
    }
    
    
    List::~List( )              // destructor: deallocate the list
    {
        cout << "List::~List( )\n";
    	delete_list( );
    }
    
    
    void List::copy_list( const List &rhs )		//Given
    {
        head = tail = 0;
                                // copy rhs' list into this
        for( Node *p = rhs.head; p; p = p->get_next( ) )
        {
            push_back( p->get_word( ) );
        }
    }
    
    
    void List::push_back( const string &s )
    {
                                // p points to a new node
        Node *p = new Node( s, 0, tail );
    
        if( tail == 0 )         // tail not pointing to a node yet?
        {
            head = tail = p;    // head & tail point to new node in the list
        }
        else
        {                       // tail->next points to new node
            tail->set_next( p );
            tail = p;           // tail points to last node in the list
        }
    }
    
    
    void List::pop_back( )
    {
    	if( tail )				// tail points to a node?
    	{
    		Node *tmp = tail;
    		tail      = tail->get_prev( );
    
    		delete tmp;			// delete node to be removed
    
    		if( tail == 0 )		// no more elements in the list?
    		{
    			head = 0;
    		}
    		else
    		{
    			tail->set_next( 0 );
    		}
    	}
    }
    
    // other methods for List...
    
    void List::delete_list( )	//ME
    {
    
        for( Node *p = head; p; )
        {
            Node *tmp = p;      // remember current pointer
            p = p->get_next( ); // advance p to the next Node
            delete tmp;         // deallocate tmp
            cout << "Deleted\t" << tmp << "\tnext is\t" << p << '\n';
        }
    }
    
    void List::push_front( const string &s )	//ME
    {
    Node *p = new Node(s,0,head);
    
        if( head == 0 )         // head not pointing to a node yet?
        {
            head = tail = p;    // head & tail point to new node in the list
        }
        else
        {                       // head->next points to new node
    		
    		p -> set_next(head);
    		head = p;
    
        }
    }
    
    void List::pop_front( )		//ME
    {
     Node* temp;  
         if(head == NULL)  
         {  
             cout<<"\nLinked list is empty";  
             return;  
         }  
         if(head->get_next() == NULL)      //to check if only one node is present  
         {  
             temp = head;  
             head = NULL;  
             //cout<<"\nDeleted node: "<<temp->data;  
             free(temp);  
          }  
         else             //If more than one nodes are present  
         {  
             temp = head;  
             head = head->get_next();  
             //cout<<"\nDeleted node: "<<temp->data;  
             free(temp);  
          }  
    }
    
    List::Iterator List::begin( ) const	// beginning of a linked list ME
    {
        return List::Iterator( head );
    }
    
    List::Iterator List::end( ) const	// end of a linked list ME
    {
        return List::Iterator( NULL );
    }
    
    
    
    
    // methods for Iterator
                                // constructor: init. an Iterator
    List::Iterator::Iterator( Node *p )
    {
        current = p;     
    	
    }
    
                                // get a const ref to word
    const string &List::Iterator::operator *( ) const
    {
        return current->get_word( );
    }
    
                                // get a const ref to word
    void List::Iterator::operator ++( )
    {
        if( current )
        {
            current = current->get_next( );
        }
    }
    
                                // current != p
    bool List::Iterator::operator !=( const Iterator &iter ) const
    {
        return current != iter.current;
    }
    
    List &List::operator =( const List &s )
    {
    	List::Iterator iter;
    
        for( iter = s.begin( ); iter != s.end( ); ++iter )
        {
            cout << "List *iter = " << *iter << "\n";
        }
    	return  (*this);
    	
    }
    Here is my header file, I'm not allowed to change it though.

    Code:
    #include    <iostream>
    #include    <iomanip>
    #include    <string>
    using namespace std;
    
    
    class Node
    {
      public:
        Node( const string &, Node *, Node * ); 
    
        const string &get_word( ) const;// get a const reference to word
        Node   *get_next( ) const;      // get a Node * (value of next)
        Node   *set_next( Node * );     // set next to a new value
        Node   *get_prev( ) const;      // get a Node * (value of prev)
        Node   *set_prev( Node * );     // set prev to a new value
    
      private:
        string   word;
        Node    *next;
        Node    *prev;
    };
    
    
    class List
    {
      public:
        List( );                        // constructor
        List( const List & );           // copy constructor
        ~List( );                       // destructor
                                        // push a node to the back of list
        void push_back( const string & );
                                        // push a node to the front of list
        void push_front( const string & );
                                        // pop  a node from the back  of list
        void pop_back( );
                                        // pop  a node from the front of list
        void pop_front( );
    
        class Iterator
        {
          public:
            Iterator( Node * = 0 );		
            const string &operator *( ) const;
            void operator ++( );
            bool operator !=( const Iterator & ) const;
    
          private:
            Node *current;
        };
    
        Iterator begin( ) const;        // pointer to beginning of the list
        Iterator end( )   const;        // pointer to end       of the list
    
      private:
    
        Node    *head;
        Node    *tail;
    
        void copy_list( const List &  );// copy a linked list
        void delete_list( );            // delete a linked list
                                        // do NOT allow copy assign. operator
        List &operator =( const List & );
    };
    And finally my main, something else I can't change. I commented everything out except cout statement.
    Code:
    #include    "List.h"
    
    // implement operator <<( ) using Iterators
    ostream &operator <<( ostream &, const List & );
    
    
    int main( )
    {
        List la;                    // create list la
    	
    	la.push_front( "mom" );
        la.push_back( "please" );
        la.push_back( "send" );
        la.push_back( "money" );
        la.push_front( "hi" );
    	
        cout << "\nla contains:\n" << la << '\n';
    	/*
        List lb( la );              // copy list la to lb
    	/*
        cout << "lb contains:\n" << lb << '\n';
    	
        lb.pop_front( );
        lb.pop_front( );
        lb.push_front( "mother" );
        lb.push_front( "dear" );
        lb.push_back( "Bubba" );
    	/*
        cout << "lb contains:\n" << lb << '\n';
    	/*
        List lc;                    // create list lc
        lc.push_back( "money" );
        lc.push_front( "send" );
    
        cout << "\nlc contains:\n" << lc << '\n';
    
        List ld;                    // create list ld
        cout << "\nld contains nothing:\n" << ld << '\n';
    
        ld.push_front( "hi" );
        cout << "ld contains:\n" << ld << '\n';
    
        ld.pop_front( );
        cout << "ld contains nothing:\n" << ld << '\n';
    
        ld.push_back( "hello" );
        ld.push_back( "Bubba" );
        cout << "ld contains:\n" << ld << '\n';
    
        ld.pop_front( );
        cout << "ld contains:\n" << ld << '\n';
    
        ld.pop_front( );
        cout << "ld contains nothing:\n" << ld << '\n';
    
        List le( ld );
        cout << "le contains nothing:\n" << le << '\n';
    
        le.push_back( "last" );
        cout << "le contains:\n" << le << '\n';
    	*/
    	system("pause");
        return 0;
    }

  2. #2
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,825

    Re: Trouble getting C++ program to print list using Iterator and Operator Overloading

    Code:
    // implement operator <<( ) using Iterators
    ostream &operator <<( ostream &, const List & );
    This is a function declaration, not a definition. You need to provide a function definition with a body.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured