Thread: Object reference not set to instance of an object

  1. #1
    Registered User
    Join Date
    Jun 2006
    Posts
    121

    Object reference not set to instance of an object

    I'd like to thank everyone for their help yesterday and the day before (Daved, you'll see that I did go to a simpler interface, without any atoi conversions to worry about). The problem I have now is this (thought I'd create a new thread for this problem): 'object reference set to an instance of an object' upon execution. The program compiles, but crashes when I try to add a line (using insertLine() function). It does not like me creating a temp linkedListNode object and trying to add the new text to its 'value' field via cin.getline(). The thought was this: set the temp node equal to head, then increment (set it equal to the next node via temp = temp->next) until reaching the node before the insertion point, then tie in the secondTmp temp node. Relevant code portions below, and then whole code posted. Thanks in advance for any help/advice!

    class declarations:
    Code:
    class linkedListNode
    {
          public:
    		     char *value;
                 linkedListNode *nextPtr;
                 linkedListNode(char *val = "", linkedListNode *nxtPtr = 0)
    			 {
    				 nextPtr = nxtPtr;
    				 value = val;
    			 }
    			 linkedListNode(linkedListNode *nextPtr)
                 {
                       
                       nextPtr = NULL;
                 }
    };
    			  
    
    class linkedList
     {
          public:
                 linkedList()
                 {
                             headPtr = tailPtr = 0;
                 }
                 ~linkedList();
                 bool isEmpty()
                 {
                      return headPtr == 0;
                 }
    			 void insertLine(int);
                 void addToHead();
                 void addToTail();
    			 void displayList();
                 void deleteFromHead();
                 void deleteNode();
    			 void deleteLines();
    			 void addTextToLine();
                 void deleteFromTail();
         private:
                 linkedListNode *headPtr, *tailPtr;
    };
    insertLine() function (the int it is passed = line number before which input is to be placed, based on user input)
    Code:
    
    void linkedList::insertLine(int num)
    {
        linkedListNode *tmp, *secondTmp; 
    	cout << "OK, enter the text to be added to line " << num << endl;
    	cin.getline(secondTmp->value, 255);   //here's where it all goes bad
        int i;
       
    	if(num == 1)
    	 {
    		 headPtr = tailPtr = tmp;
    	 }
    	 else 
    	 {   
    		 tmp = headPtr;
             for(i = 1; i < num; i++) 
             {
               tmp = tmp->nextPtr;
             }
    	 }
    	 tmp->nextPtr = secondTmp;
    	 secondTmp->nextPtr = tmp->nextPtr->nextPtr;
    }
    __________________________________________________ ______________
    whole program:
    Code:
    #include <iostream>
    #include <string>
    #include <cstring>
    #include <cstdlib>
    using namespace std;
    
    class linkedListNode
    {
          public:
    		     char *value;
                 linkedListNode *nextPtr;
                 linkedListNode(char *val = "", linkedListNode *nxtPtr = 0)
    			 {
    				 nextPtr = nxtPtr;
    				 value = val;
    			 }
    			 linkedListNode(linkedListNode *nextPtr)
                 {
                       
                       nextPtr = NULL;
                 }
    };
    			  
    
    class linkedList
     {
          public:
                 linkedList()
                 {
                             headPtr = tailPtr = 0;
                 }
                 ~linkedList();
                 bool isEmpty()
                 {
                      return headPtr == 0;
                 }
    			 void insertLine(int);
                 void addToHead();
                 void addToTail();
    			 void displayList();
                 void deleteFromHead();
                 void deleteNode();
    			 void deleteLines();
    			 void addTextToLine();
                 void deleteFromTail();
         private:
                 linkedListNode *headPtr, *tailPtr;
    }; 
                             
    
    linkedList::~linkedList()
    {
        for(linkedListNode *p; !isEmpty(); )
        {
                           p = headPtr->nextPtr;
                           delete headPtr;
                           headPtr = p;
        }
    }
    
    void linkedList::addToHead()
    {
    	char headTxt[255];
    	cout << "Please enter the text to be inserted into the first line: " << endl;
    	cin.getline(headTxt, 255);
         headPtr = new linkedListNode(headTxt, headPtr);
         if(tailPtr == 0)
            tailPtr = headPtr;
    }
    
    
    void linkedList::addToTail() 
      {  
         linkedListNode *temp, *nextTemp;   
         char input[255]; 
         cout << "Please enter the line of text to be added: ";
         cin.getline(input,255);
    	 cin.ignore();
    	 temp = new linkedListNode();
         temp->nextPtr = NULL;
    
         if (headPtr == NULL) 
            { 
               headPtr = temp;
            }
         else 
           {
               nextTemp = headPtr;
               while (nextTemp->nextPtr != NULL)
               {  
                     nextTemp = nextTemp->nextPtr; 
               }
               nextTemp->nextPtr = temp;
           }
      }
    
    void linkedList::insertLine(int num)
    {
        linkedListNode *tmp, *secondTmp; 
    	cout << "OK, enter the text to be added to line " << num << endl;
    	cin.getline(secondTmp->value, 255);
        int i;
       
    	if(num == 1)
    	 {
    		 headPtr = tailPtr = tmp;
    	 }
    	 else 
    	 {   
    		 tmp = headPtr;
             for(i = 1; i < num; i++) 
             {
               tmp = tmp->nextPtr;
             }
    	 }
    	 tmp->nextPtr = secondTmp;
    	 secondTmp->nextPtr = tmp->nextPtr->nextPtr;
    }
    
    void linkedList::deleteFromHead()
    {
         linkedListNode *tmp = headPtr;
         if(headPtr == tailPtr) //if only one node in the list;
             headPtr = tailPtr = 0;
         else headPtr = headPtr->nextPtr;
         delete tmp;
    }
    
    
    
    void linkedList::deleteNode()
    {
    	 int lineNum;
    	 cout << "Which line number would you like to delete?: " << endl;
    	 cin >> lineNum;
         cin.ignore();
         linkedListNode *pred, *tmp;
         int i;
    	 pred = headPtr;
    	 tmp = headPtr->nextPtr;
         for(i = 1; i < lineNum; i++)
         {
               pred = pred->nextPtr;
               tmp = tmp->nextPtr;
         }
         if(tmp != 0)
         {
                pred->nextPtr = tmp->nextPtr;
                if(tmp == tailPtr)
                tailPtr = pred;
                delete tmp;
         }
    }
    
    void linkedList::deleteLines()
    {
    	int startLine, endLine;
    	cout << "Which is your first desired line of deletion?: " << endl;
    	cin >> startLine;
    	cin.ignore();
    	cout << "\nAnd where do you wish to end?: " << endl;
    	cin >> endLine;
    	cout << endl;
         linkedListNode *pred, *tmp; //start here
         int i;
    	 pred = headPtr;
    	 tmp = headPtr->nextPtr;
         for(i = 1; i < startLine; i++)
         {
               pred = pred->nextPtr;
               tmp = tmp->nextPtr;
         }
         if(tmp != 0)
    	 {
    		 for(i = startLine; i <= endLine; i++)
                 {
                     pred->nextPtr = tmp->nextPtr;
                     if(tmp == tailPtr)
                     tailPtr = pred;
                     delete tmp;
                 }
    	 }
    }
    
    void linkedList::addTextToLine()
    {
    	 int lineNum;
    	 cout << "Which line would you like to append to?" << endl;
    	 cin >> lineNum;
    	 if(lineNum == 1)
    	 {
    		 addToHead();
    	 }
    	 char appTxt[255];
    	 cout << "Enter the text to be appended to line " << lineNum << endl;
    	 cin.getline(appTxt, 255);
         linkedListNode *tmp;
    	 tmp = headPtr;
    	 int i;
         for(i = 1; i <= lineNum; i++)
         {
               tmp = tmp->nextPtr;
         }
         strcat(tmp->value, appTxt);
    }
    
    void linkedList::displayList()
    {
    	linkedListNode *temp;
     int line = 1;
         //Loop to display the content sof the list as long as the list has data
             while (temp != NULL)
             {  
                  // Display details for what temp points to
                  cout << line << "> " << temp->nextPtr << " ";
                  line++;
                  cout << endl; // Line break
    	          temp = temp->nextPtr;
             }
    	     cout << "This marks the end of the list" << endl;
    }
    int main()
    {
    	int lineNum;
    	linkedList l;
    	char response;
        do
    	{
    	  l.displayList();
    	  cout << endl;
    	  cout << "Here are your options : " << endl;
    	  cout << "E - Exit program." << endl;
    	  cout << "I - Insert a line." << endl;
    	  cout << "D - Delete a line." << endl;
    	  cout << "A - Append text to a line." << endl;
    	  cout << endl << " >> ";
    	  cin >> response;
    	  cin.ignore();
    
    	  if(toupper(response) == 'E')
    	  {
    		  exit(1);
    	  }
    	  else if(toupper(response) == 'I')
    	  { 	  
    		  cout << "OK, enter the line number before which you would like " << endl;
    		  cout << " to insert text: " << endl;
    		  cin >> lineNum;
    		  if(lineNum == 1)
    		  {
                  l.addToHead();
    		  }
    		  else if(lineNum == 0)
    		  {
    			  cout << "Line 1 or higher, please!" << endl;
    		  }
    		  else
    		  {
    		  l.insertLine(lineNum);
    		  }
    	  }
    	 
    	  else if(toupper(response) == 'D')
    	  {
    		   char numberLines;
    		   cout << "Would you like to delete one or more than on line(s)? " << endl;
    		   cout << "A for 1, B for more than one: ";
    		   cin >> numberLines;
    		   cin.ignore();
                  here:
    		   if(toupper(numberLines) == 'A')
    		   {
    			   l.deleteNode();
    		   }
    		   else if(toupper(numberLines) == 'B')
    		   {
    			   l.deleteLines();
    		   }
    		   else
    		   {
    			   cout << "Incorrect input! " << endl;
    			   goto here;
    		   }
    	  }
    
    	  else if(toupper(response == 'A'))
    	  {
    		  l.addTextToLine();
    	  }
    	  else
    	  {
    		  cout << "Sorry, that was incorrect input.  Please follow the instructions " << endl;
    		  cout << " below carefully!" << endl;
    	  }
    	}
         while (response != 0);
    }

  2. #2
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    Code:
    void linkedList::insertLine(int num)
    {
        linkedListNode *tmp, *secondTmp;           // uninitialized pointers
    	cout << "OK, enter the text to be added to line " << num << endl;
    	cin.getline(secondTmp->value, 255);   //here's where it all goes bad
                                                   // writing to random location --> boom
    also your nodes class is less then ideal. It doesn't hold the data. This way you have to manage the data that the nodes point to from outside of the node class. That limits the usability of the list quite a lot.
    If you look e.g. at your addToHead() function. You construct a node that is initialized with a pointer to a local buffer. upon exit of the function this buffer doesn't exist anymore so the pointer in the node points to garbage.
    I would say that you shoud allocate space for the data inside the constructor and copy the data.
    This way you would need a destructor as well ( to be able to release the allocated space ). A copy constructor and assignement operator would be needed as well.
    Better even would be to make the nodes data a std::string.
    Kurt

  3. #3
    Registered User
    Join Date
    Jun 2006
    Posts
    121
    __________________________________________________ _________________________
    Thanks for the reply, Kurt. If I just declared 'value' to be a string and used cin, it would stop at the first space encountered, right? I've added copy constructor, assignment operator and dsestructors to the listkedListNode class:
    Code:
    linkedListNode(const linkedListNode& l)//copy constructor
    			 {
    
    				 nextPtr = l.nextPtr;
    				 value = strdup(l.value);
    			 }
    			 linkedListNode& operator=(const linkedListNode& l) //assignment op.
    			 {
    				 if(this != &l) //make sure not to assign to itself
    				 {
    					 if(value != 0)
    					 {
    						 delete []value;
    					 }
    					 value = strdup(l.value);
                         nextPtr = l.nextPtr;
    				 }
    			 }
                 ~linkedListNode()
    			 {
                     if(value != 0)
    				 {
    					 delete [] value;//to prevent dangling reference
    				 }
    			 }
    Does that look right? I think that's appropriate for the linkedListNode class.
    Also, what do you mean by,
    I would say that you shoud allocate space for the data inside the constructor and copy the data.
    ?

  4. #4
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    It shure looks better now. You do copy the data now in the constructors ( by using strdup() ).
    What I'm not shure about is if its ok to delete [] data. As far as I know strdup() allocates the memory using c-style malloc and you release it C++-style. guess you should use
    Code:
    free( data );
    If I just declared 'value' to be a string and used cin, it would stop at the first space encountered, right?
    yes it would, but it's the same with operator >>, no matter if its a string or an array of characters. You could use getline.
    Kurt

  5. #5
    Registered User
    Join Date
    Jun 2006
    Posts
    121
    Quote Originally Posted by ZuK
    [CODE]void linkedList::insertLine(int num)
    {
    linkedListNode *tmp, *secondTmp; // uninitialized pointers
    cout << "OK, enter the text to be added to line " << num << endl;
    cin.getline(secondTmp->value, 255); //here's where it all goes bad

    Kurt
    Kurt, my new code below. I tried secondTmp = new linkedListNode; but that didn't seem to be equivalent to an 'initialization'. What, then, does the compiler consider to be initialization?

    -Patrick

    Code:
    #include <iostream>
    #include <string>
    #include <cstring>
    #include <cstdlib>
    #include <cassert>
    using namespace std;
    
    class linkedListNode
    {
          public:
    		     char *value;
                 linkedListNode *nextPtr;
                 linkedListNode(char *val = "", linkedListNode *nxtPtr = 0)
    			 {
    				 nextPtr = nxtPtr;
    				 value = val;
    			 }
    			 linkedListNode(linkedListNode *nextPtr)
                 {
                       
                       nextPtr = NULL;
                 }
                 linkedListNode& operator=(const linkedListNode&);
    			 linkedListNode(const linkedListNode&);
    			 ~linkedListNode();
    };
    			  
    
    class linkedList
     {
          public:
                 linkedList()
                 {
                             headPtr = tailPtr = 0;
                 }
                 ~linkedList();
                 bool isEmpty()
                 {
                      return headPtr == 0;
                 }
    			 void insertLine(int);
                 void addToHead();
                 void addToTail();
    			 void displayList();
                 void deleteFromHead();
                 void deleteNode();
    			 void deleteLines();
    			 void addTextToLine();
                 void deleteFromTail();
         private:
                 linkedListNode *headPtr, *tailPtr;
    }; 
    
    linkedListNode::linkedListNode(const linkedListNode& l)//copy constructor
    {
    
       nextPtr = l.nextPtr;
       value = strdup(l.value);
    }
    linkedListNode& linkedListNode::operator=(const linkedListNode& l) //assignment op.
    {
    	 if(this != &l) //make sure not to assign to itself
    		 {
    		 if(value != 0)
    		 {
    		 free(value);
    		 }
    		 value = strdup(l.value);
             nextPtr = l.nextPtr;
    		 }
        return *this;
    }
    linkedListNode::~linkedListNode()
    {
        if(value != 0)
        {
        free(value);//to prevent dangling reference
        }
    }                        
    
    linkedList::~linkedList()
    {
        for(linkedListNode *p; !isEmpty(); )
        {
                           p = headPtr->nextPtr;
                           delete headPtr;
                           headPtr = p;
        }
    }
    
    void linkedList::addToHead()
    {
    	char headTxt[255];
    	cout << "Please enter the text to be inserted into the first line: " << endl;
    	cin.getline(headTxt, 255);
         headPtr = new linkedListNode(headTxt, headPtr);
         if(tailPtr == 0)
            tailPtr = headPtr;
    }
    
    
    void linkedList::addToTail() 
      {  
         linkedListNode *temp, *nextTemp;   
         char input[255]; 
         cout << "Please enter the line of text to be added: ";
         cin.getline(input,255);
    	 cin.ignore();
    	 temp = new linkedListNode();
         temp->nextPtr = NULL;
    
         if (headPtr == NULL) 
            { 
               headPtr = temp;
            }
         else 
           {
               nextTemp = headPtr;
               while (nextTemp->nextPtr != NULL)
               {  
                     nextTemp = nextTemp->nextPtr; 
               }
               nextTemp->nextPtr = temp;
           }
      }
    
    void linkedList::insertLine(int num)
    {
    	char newLineText[255];
        linkedListNode *tmp, *secondTmp; 
    	cout << "OK, enter the text to be added to line " << num << endl;
    	cin.getline(newLineText, 255);
    	secondTmp = new linkedListNode;
    	tmp = new linkedListNode;
    	assert(secondTmp != NULL);
    	secondTmp->value = newLineText;
        int i;
       
    	if(num == 1)
    	 {
    		 headPtr = tailPtr = tmp;
    	 }
    	 else 
    	 {   
    		 tmp = headPtr;
             for(i = 1; i < num; i++) 
             {
               tmp = tmp->nextPtr;
             }
    	 }
    	 tmp->nextPtr = secondTmp;
    	 secondTmp->nextPtr = tmp->nextPtr->nextPtr;
    }
    
    void linkedList::deleteFromHead()
    {
         linkedListNode *tmp = headPtr;
         if(headPtr == tailPtr) //if only one node in the list;
             headPtr = tailPtr = 0;
         else headPtr = headPtr->nextPtr;
         delete tmp;
    }
    
    
    
    void linkedList::deleteNode()
    {
    	 int lineNum;
    	 cout << "Which line number would you like to delete?: " << endl;
    	 cin >> lineNum;
         cin.ignore();
         linkedListNode *pred, *tmp;
         int i;
    	 pred = headPtr;
    	 tmp = headPtr->nextPtr;
         for(i = 1; i < lineNum; i++)
         {
               pred = pred->nextPtr;
               tmp = tmp->nextPtr;
         }
         if(tmp != 0)
         {
                pred->nextPtr = tmp->nextPtr;
                if(tmp == tailPtr)
                tailPtr = pred;
                delete tmp;
         }
    }
    
    void linkedList::deleteLines()
    {
    	int startLine, endLine;
    	cout << "Which is your first desired line of deletion?: " << endl;
    	cin >> startLine;
    	cin.ignore();
    	cout << "\nAnd where do you wish to end?: " << endl;
    	cin >> endLine;
    	cout << endl;
         linkedListNode *pred, *tmp; //start here
         int i;
    	 pred = headPtr;
    	 tmp = headPtr->nextPtr;
         for(i = 1; i < startLine; i++)
         {
               pred = pred->nextPtr;
               tmp = tmp->nextPtr;
         }
         if(tmp != 0)
    	 {
    		 for(i = startLine; i <= endLine; i++)
                 {
                     pred->nextPtr = tmp->nextPtr;
                     if(tmp == tailPtr)
                     tailPtr = pred;
                     delete tmp;
                 }
    	 }
    }
    
    void linkedList::addTextToLine()
    {
    	 int lineNum;
    	 cout << "Which line would you like to append to?" << endl;
    	 cin >> lineNum;
    	 if(lineNum == 1)
    	 {
    		 addToHead();
    	 }
    	 char appTxt[255];
    	 cout << "Enter the text to be appended to line " << lineNum << endl;
    	 cin.getline(appTxt, 255);
         linkedListNode *tmp;
    	 tmp = headPtr;
    	 int i;
         for(i = 1; i <= lineNum; i++)
         {
               tmp = tmp->nextPtr;
         }
         strcat(tmp->value, appTxt);
    }
    
    void linkedList::displayList()
    {
    	linkedListNode *temp;
    	temp = new linkedListNode;
     int line = 1;
         //Loop to display the content sof the list as long as the list has data
             while (temp != NULL)
             {  
                  // Display details for what temp points to
                  cout << line << "> " << temp->nextPtr << " ";
                  line++;
                  cout << endl; // Line break
    	          temp = temp->nextPtr;
             }
    	     cout << "This marks the end of the list" << endl;
    }
    int main()
    {
    	int lineNum;
    	linkedList l;
    	char response;
        do
    	{
    	  l.displayList();
    	  cout << endl;
    	  cout << "Here are your options : " << endl;
    	  cout << "E - Exit program." << endl;
    	  cout << "I - Insert a line." << endl;
    	  cout << "D - Delete a line." << endl;
    	  cout << "A - Append text to a line." << endl;
    	  cout << endl << " >> ";
    	  cin >> response;
    	  cin.ignore();
    
    	  if(toupper(response) == 'E')
    	  {
    		  exit(1);
    	  }
    	  else if(toupper(response) == 'I')
    	  { 	  
    		  cout << "OK, enter the line number before which you would like " << endl;
    		  cout << " to insert text: " << endl;
    		  cin >> lineNum;
    		  if(lineNum == 1)
    		  {
                  l.addToHead();
    		  }
    		  else if(lineNum == 0)
    		  {
    			  cout << "Line 1 or higher, please!" << endl;
    		  }
    		  else
    		  {
    		  l.insertLine(lineNum);
    		  }
    	  }
    	 
    	  else if(toupper(response) == 'D')
    	  {
    		   char numberLines;
    		   cout << "Would you like to delete one or more than on line(s)? " << endl;
    		   cout << "A for 1, B for more than one: ";
    		   cin >> numberLines;
    		   cin.ignore();
                  here:
    		   if(toupper(numberLines) == 'A')
    		   {
    			   l.deleteNode();
    		   }
    		   else if(toupper(numberLines) == 'B')
    		   {
    			   l.deleteLines();
    		   }
    		   else
    		   {
    			   cout << "Incorrect input! " << endl;
    			   goto here;
    		   }
    	  }
    
    	  else if(toupper(response == 'A'))
    	  {
    		  l.addTextToLine();
    	  }
    	  else
    	  {
    		  cout << "Sorry, that was incorrect input.  Please follow the instructions " << endl;
    		  cout << " below carefully!" << endl;
    	  }
    	}
         while (response != 0);
    }

  6. #6
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    Your list class is too big that I would want to debug it.
    So I have created a simple list class to demonstrate a possible use of your node class as it is.
    Code:
    #include <iostream>
    #include <string>
    #include <cstring>
    #include <cstdlib>
    #include <cassert>
    using namespace std;
    
    class linkedListNode
    {
          public:
    	char *value;
            linkedListNode *nextPtr;
            linkedListNode(char *val = "", linkedListNode *nxtPtr = 0)	 {
    	         nextPtr = nxtPtr;
    		 value = strdup(val);   // copy the passed data
    	 }
    	linkedListNode(linkedListNode *nextPtr) // don't understand what you want to do with this one
            {                   
                       nextPtr = NULL;
             }
             linkedListNode& operator=(const linkedListNode&);
    	linkedListNode(const linkedListNode&);
    	~linkedListNode();
    };
    			  
    
    class linkedList
     {
          public:
                 linkedList()
                 {
                             headPtr = tailPtr = 0;
                 }
                 ~linkedList();
                 bool isEmpty()
                 {
                      return headPtr == 0;
                 }
    			 void add(const linkedListNode & node );
    			 linkedListNode * get( int idx );
    			 void display();
         private:
                 linkedListNode *headPtr, *tailPtr;
    }; 
    
    linkedListNode::linkedListNode(const linkedListNode& l)//copy constructor
    {
       nextPtr = l.nextPtr;
       value = strdup(l.value);
    }
    linkedListNode& linkedListNode::operator=(const linkedListNode& l) //assignment op.
    {
    	 if(this != &l) //make sure not to assign to itself
    		 {
    		 if(value != 0)
    		 {
    		 free(value);
    		 }
    		 value = strdup(l.value);
             nextPtr = l.nextPtr;
    		 }
        return *this;
    }
    
    linkedListNode::~linkedListNode()
    {
        if(value != 0)
        {
        free(value);//to prevent dangling reference
        }
    }                        
    
    linkedList::~linkedList()
    {
        while ( headPtr ) {
    	   linkedListNode *p = headPtr;
    	   headPtr = headPtr->nextPtr;
    	   delete p;
    	}
    }
    
    void linkedList::add( const linkedListNode & node  ) {
        if ( 0 == headPtr ) {
    	   headPtr = new linkedListNode( node );
    	   tailPtr = headPtr;
    	}
    	else {
    	   tailPtr->nextPtr = new linkedListNode( node );
    	   tailPtr = tailPtr->nextPtr;
    	}
    }
    
    void linkedList::display( ) {
        linkedListNode * node = headPtr;
    	int idx = 0;
    	while ( node ) {
    	    cout << idx++ << ": '" << node->value << "'" << endl;
    	    node = node->nextPtr;
    	}
    }
    
    linkedListNode * linkedList::get( int idx ) {
       linkedListNode * pnode = headPtr;
       while ( idx-- ) {
          pnode = pnode->nextPtr;
    	  if ( ! pnode )
    	      break;
       }
       return pnode;
    }
    
    int main()
    {
    	linkedList l;
    	char buffer[256];
    	// create 3 nodes
    	for ( int i = 0; i < 3; ++ i ) {
    	    cout << " enter data for node " << i << " : " ;
    	    cin.getline(buffer, 256);
    		linkedListNode node( buffer );
    		l.add( node );
    	}	
    	cout << endl;
    	l.display();
    	cout << endl;
    	// change 2nd node
    	int idx = 1;
    	linkedListNode *pnode = l.get(idx);
    	if ( pnode ) {
    	    cout << "changing node " << idx << " : '" << pnode->value << "' : ";
    		cin.getline(buffer, 256);	
    		*pnode = linkedListNode(buffer, pnode->nextPtr ); // calls assignement operator
    	}
    	l.display();
    }
    I have not tested this example very well so there might be bugs.
    Kurt
    EDIT: and yes the things that I have put into main should be member functions of the list.
    Last edited by ZuK; 10-26-2006 at 04:36 AM.

  7. #7
    Registered User
    Join Date
    Jun 2006
    Posts
    121
    Yeah, your code is pretty much bug-free, from what I can tell. Now I have to sit down and figure out what I wasn't doing/was doing wrong. I did take the extra default constructor out, BTW; I had put it there upon a suggestion from someone here. So, was it the assignment 'value = val;' that was crashing the program at that point? I was using strdup before, but the compiler didn't like it for some reason, and I changed it. Thanks for the corrections!

    -Patrick

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 05-13-2011, 08:28 AM
  2. how to get the reference of the object in C++
    By maxcat in forum Windows Programming
    Replies: 6
    Last Post: 03-06-2008, 07:27 PM
  3. Replies: 4
    Last Post: 01-23-2008, 06:21 AM
  4. circular doubly linked list help
    By gunnerz in forum C++ Programming
    Replies: 5
    Last Post: 04-28-2007, 08:38 PM
  5. My graphics library
    By stupid_mutt in forum C Programming
    Replies: 3
    Last Post: 11-26-2001, 06:05 PM