Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

linklist class problem

Options
  • 18-03-2009 9:32pm
    #1
    Registered Users Posts: 401 ✭✭


    Hi all,

    I have a problem with a code im writing

    using Visual studio 2008.

    Compiles fine but throws up this when i run it
    Unhandled exception at 0x0041222e in OOP-lab7.exe: 0xC0000005: Access violation reading location 0xfeeefeee.
    

    the error happens after the overloaded + operand is used
    Edit: should just update this, the error happens the next time i try do anything with the object be in ll.count, ll.max etc

    here is the code:
    #include "stdafx.h"
    #include <iostream>
    
    using namespace std;
    
    class list
    {
    
    private:
    
             struct node
             {
                 int data;
                node *link;
             }*p;
    public:
    
             list();
    		 ~list();
    
             void append( int num );
             void del( int num );
             void display();
             int count();
    		 int max();
    		 int min();
    		 int average();
    		 void search(int num);
    
    		 friend list  operator+(list &l1, int num); 
             
    };
    
    list::list()
    {
         p=NULL;
    }
    
    list::~list()
    {
         node *q;
       if( p == NULL )
            return;
    
       while( p != NULL )
       {
            q = p->link;
          delete p;
          p = q;
       }
    }
    
    void list::append(int num)
    {
         node *q,*t;
    
       if( p == NULL )
       {
            p = new node;
          p->data = num;
          p->link = NULL;
       }
       else
       {
            q = p;
          while( q->link != NULL )
               q = q->link;
    
          t = new node;
          t->data = num;
          t->link = NULL;
          q->link = t;
       }
    }
    
    
    
    
    void list::del( int num )
    {
         node *q,*r;
       q = p;
       if( q->data == num )
       {
            p = q->link;
          delete q;
          return;
       }
    
       r = q;
       while( q!=NULL )
       {
            if( q->data == num )
          {
               r->link = q->link;
             delete q;
             return;
          }
    
          r = q;
          q = q->link;
       }
       cout<<"\nElement "<<num<<" not Found.";
    }
    
    void list::display()
    {
         node *q;
       cout<<endl;
    
       for( q = p ; q != NULL ; q = q->link )
            cout<<endl<<q->data;
    
    }
    
    int list::count()
    {
         node *q;
       int c=0;
       for( q=p ; q != NULL ; q = q->link )
            c++;
    
       return c;
    }
    
    int list::max()
    {
    	node *q;
    	int max=0;
    	q=p;
    	max = q->data;
    	for( q ; q != NULL ; q = q->link )
    	{
    	   if (q->data > max)
    	   {
    		   max = q->data;
    
    	   }
    	}
    	    
    	return max;
    }
    
    int list::min()
    {
    	int min=0;
    	node *q;
    
    
    	q=p;
    
    	min = q->data;
    
    	
    	for( q; q != NULL ; q = q->link )
    	{
    	   if (q->data < min)
    	   {
    		   min = q->data;
    
    	   }
    	}
    	    
    	return min;
    }
    int list::average()
    {
    	node *q;
    	int total=0;
    
    	for( q=p ; q != NULL ; q = q->link )
    	{
    	   total += q->data;
    	}
    
    	total = total/count();
    	    
    	return total;
    }
    void list::search(int num)
    {
    	node *q;
    	
    	for( q=p; q != NULL ; q = q->link )
    	{
    	   if (q->data == num)
    	   {
    		   
    			cout<<"\nElement "<<num<<" Found.";
    
    	   }
    	}
    	return;
    }
    
    
    
    
    int main()
    {
         list ll;
       cout<<"No. of elements = "<<ll.count();
       ll.append(13);
       ll.append(12);
       ll.append(23);
       ll.append(100);
       ll.append(44);
       ll.append(50);
    
    
       ll.display();
       cout<<"\nNo. of elements = "<<ll.count();
    
       ll.del(333);
       ll.del(12);
       ll.del(98);
       ll = ll + 60;
      // cout<<"\nNo. of elements = "<<ll.count();
    	cout<<"\nMax = "<<ll.max();
    	cout<<"\nMin = "<<ll.min();
    	cout<<"\naverage = "<<ll.average();
    
    	ll.search(50);
    
       return 0;
    }
    
    
    list  operator+(list &l1, int num)
    {
    
    	l1.append(num);
    
    	return l1;
    }
    

    Any ideas where the error is coming from?

    Thanks for any advice you can give


Comments

  • Closed Accounts Posts: 3,357 ✭✭✭Beano


    you dont seem to have instantiated the class in your main() function.


  • Closed Accounts Posts: 60 ✭✭LARDO


    ll = ll + 60; doesnt make any sense.

    first of all "ll + 60;" does what u want ie adding 60 to the list but of course this isnt really what the operator + is meant for, the operator + return a list object say b, now u have a = b, but u dont have a copy constructor or overloaded = operator, if these were pointers to objects the statement would work but that causes problems for the + operator then!

    also ur operator wont work with statements like ll+ 60 + 70, the secont + call would translate to operator+(60,70); and would fail because u dont have a constructor list(int x);

    basically u need some type casting operators a copy constructor an overloaded = operator etc... hope this helps!!


  • Registered Users Posts: 401 ✭✭sharkDawg


    Lardo your bang on with the copy constructor , a friend of mine figured it out this morning.

    Here is the updated code for reference(probably not great but it works! and unfortunately its all the time I have this week to spare on it.)

    Note: Apoligies for lack of comments, its a very busy week!
    
    #####################################################*/
    
    
    #include "stdafx.h"
    #include <iostream>
    
    using namespace std;
    
    class list
    {
    
    private:
    
             struct node
             {
                 int data;
                node *link;
             }*p;
    public:
    
             list();
    		 ~list();
    		 list(const list &temp);
    
             void append( int num );
             void del( int num );
             void display();
    		 list &list::operator=(const list &temp);
    		 void addList(const list &temp);
             int count();
    		 int max();
    		 int min();
    		 int average();
    		 void search(int num);
    
    		 friend list  operator+(const list &l1, int num); 
    		 friend list  operator+(list &l1, list&l2);
             
    };
    
    list::list()
    {
         p=NULL;
    }
    
    list::~list()
    {
         node *q;
       if( p == NULL )
            return;
    
       while( p != NULL )
       {
            q = p->link;
          delete p;
          p = q;
       }
    }
    
    list::list(const list &temp)
    {
    	p = NULL;
    	for(node *q=temp.p; q != NULL ; q = q->link )
    	{
    		append(q->data);
    	}
    }
    
    //function for adding numbers to the list
    void list::append(int num)
    {
         node *q,*t;
    
       if( p == NULL )
       {
            p = new node;
          p->data = num;
          p->link = NULL;
       }
       else
       {
            q = p;
          while( q->link != NULL )
    	  {
    		  q = q->link;
    	  }
    
          t = new node;
          t->data = num;
          t->link = NULL;
          q->link = t;
       }
    }
    
    
    
    //function for delteting numbers from the list
    void list::del( int num )
    {
         node *q,*r;
       q = p;
       if( q->data == num )
       {
            p = q->link;
          delete q;
          return;
       }
    
       r = q;
       while( q!=NULL )
       {
            if( q->data == num )
          {
               r->link = q->link;
             delete q;
             return;
          }
    
          r = q;
          q = q->link;
       }
      
    }
    
    list &list::operator=(const list &temp)
    {
    	//clean the existing list
    	while( p != NULL )
    	{
    		node *q = p->link;
    		delete p;
    		p = q;
    	}
    
    	//copy the date to the now cean one
    	p = NULL;
    	for(node *q=temp.p; q != NULL ; q = q->link )
    	{
    		append(q->data);
    	}
    
    	return *this;
    }
    
    //function for printing the list
    void list::display()
    {
         node *q;
       cout<<endl;
    
       for( q = p ; q != NULL ; q = q->link )
            cout<<endl<<q->data;
    
    }
    
    //function for counting the number of elements in a list
    int list::count()
    {
         node *q;
       int c=0;
       for( q=p ; q != NULL ; q = q->link )
            c++;
    
       return c;
    }
    
    //function for adding the contents of one list to another
    void list::addList(const list &temp)
    {
    	node *q;
    	q =temp.p;
    	if( temp.p == NULL )
       {
    		cout<<"\nNo numbers found. Returning 0";
    		return;
    	}
    
    	
    	for( q; q != NULL ; q = q->link )
    	{
    	   append(q->data);
    	}
      
    }
    
    //function for finding the largest number in a list
    int list::max()
    {
    	
    	node *q;
    	int max=0;
    	q=p;
    	if( p == NULL )
       {
    		cout<<"\nNo numbers found. Returning 0";
    		return 0;
    	}
    	max = q->data;
    	for( q ; q != NULL ; q = q->link )
    	{
    	   if (q->data > max)
    	   {
    		   max = q->data;
    
    	   }
    	}
    	    
    	return max;
    }
    
    //function for finding the smallest number in a list
    int list::min()
    {
    	int min=0;
    	node *q;
    
    
    	q=p;
    
    	if( p == NULL )
       {
    		cout<<"\nNo numbers found. Returning 0";
    		return 0;
    	}
    
    	min = q->data;
    
    	
    	for( q; q != NULL ; q = q->link )
    	{
    	   if (q->data < min)
    	   {
    		   min = q->data;
    
    	   }
    	}
    	    
    	return min;
    }
    
    //function for finding the average of the numbers in the list
    int list::average()
    {
    	node *q;
    	int total=0;
    	q=p;
    	if( p == NULL )
       {
    		cout<<"\nNo numbers found. Returning 0";
    		return 0;
    	}
    
    	for( q=p ; q != NULL ; q = q->link )
    	{
    	   total += q->data;
    	}
    
    	total = total/count();
    	    
    	return total;
    }
    
    //function for searching for a certain value in the list
    void list::search(int num)
    {
    	node *q;
    	
    	for( q=p; q != NULL ; q = q->link )
    	{
    	   if (q->data == num)
    	   {
    		   
    			cout<<"\nElement "<<num<<" Found.";
    
    	   }
    	}
    	return;
    }
    
    
    
    
    
    
    int main()
    {
    	list ll;
    	list l2;
    	 
    	cout<<"No. of elements = "<<ll.count();
    
    	ll.append(13);
    	ll.append(12);
    	ll.append(23);
    
    	cout<<"\nPrinting contents of ll"<<endl;
    
    	ll.display();
    
    	l2.append(100);
    	l2.append(44);
    	l2.append(50);
    
    	cout<<"\nPrinting contents of l2"<<endl;
    
    	l2.display();
    
    	cout<<"\nAdding contents of l2 to ll"<<endl;
    
    	ll = ll + l2;
    
    	ll.display();
    
    	cout<<"\nNo. of elements = "<<ll.count();
    
    
    	cout<<"\nAdding 60 to ll"<<endl;
    	ll = ll + 60;
    	ll.display();
    
    	cout<<"\nMax = "<<ll.max();
    	cout<<"\nMin = "<<ll.min();
    	cout<<"\naverage = "<<ll.average();
    
    	cout<<"\nSearching for 50"<<endl;
    	ll.search(50);
    
    
    	return 0;
    }
    
    
    list  operator+(list &l1, list&l2)
    {
    	list result = l1;
    	result.addList(l2);
    
    	return result;
    }
    
    list operator+(const list &l1, int num)
    {
    	list result = l1;
    	result.append(num);
    
    	return result;
    }
    


  • Closed Accounts Posts: 60 ✭✭LARDO


    for what its worth dude i would change append to list ,
    ie list::list instead of list::append, i think the logic would read better and u
    have a new constructor into the bargain which would prevent errors in certain contexts. After all append should add an existing object to the list end not create a new object. Object creation should be done in constructors.


Advertisement