?
Solved

Destructor error

Posted on 2004-04-21
5
Medium Priority
?
464 Views
Last Modified: 2008-03-17
I created the following program in VC++ 6.0.
Without the destructor everything compiles and runs fine.

This is my compilation error with the Destructor:

compiling...
Prog6-2167.cpp
c:\2613\p06\prog6-2167.cpp(276) : fatal error C1001: INTERNAL COMPILER ERROR
        (compiler file 'msc1.cpp', line 1794)
         Please choose the Technical Support command on the Visual C++
         Help menu, or open the Technical Support help file for more information
Error executing cl.exe.

Prog6-2167.exe - 1 error(s), 0 warning(s)



Heres is my somewhat lengthy code
The destructor is defined at the bottom:


#include<iostream>
#include<fstream>
#include<string>
#include<iomanip>
#include<new>


using namespace std;

struct Node

      {

             string word;        // the word  

             int wordCount;      // times the words has occurred

             Node *nextPtr;      // points to the next node

      };



class List
{
private:
      Node *head;   //List head pointer
      Node * createNode(string);
public:
   
      List()   {head=NULL;};
      ~List();   //Destructor  
    void loadNode(string str);
    void findMax();
};


bool openfile(ifstream &, char [20]);
void DisplayFile();

int main()
{

      DisplayFile();
      List list;

      ifstream ifile;

if (!openfile(ifile,"inputfile.txt"))
{
      cout<<"Error opening inputfile.txt "<<endl;
      exit(1);
}
     
   

  string line;
 
int word_size=0;
int word_count=0;
  while(ifile>>line)
  {
        
        for(int i=0;i<line.size();i++)
        {
              if(toupper(line[i])==tolower(line[i]))  //omits punctuation
              {
                    line.erase(i,1);
                 
              }
         
        }
  word_count++;
  word_size+=line.size();
   list.loadNode(line);
   
  }
  list.findMax();
  cout<<"The total number of words: "<<word_count<<endl;
  cout<<"Average word size: "<<word_size/word_count<<endl;
 
 
 

return 0;
}

//---------------------------------------------------------
//Function createNode uses the new operator to
//create a Node. It then returns the Node to be added
//to the list.
//----------------------------------------------------------
  Node * List::createNode(string ptr)
{
      Node *newNode;
      newNode=new Node;
      newNode->word=ptr;
      newNode->wordCount=1;
      newNode->nextPtr=NULL;
      return newNode;
}

//-----------------------------------------------------
//Function openfile opens the input file
//-----------------------------------------------------

  bool openfile(ifstream &file, char filename[20])
{
      bool status;
      file.open(filename);
      if (file.fail())
            status=false;
      else
            status=true;
      return status;
}


//---------------------------------------------------------------------------
//Function loadNode takes a string form the input file and creates a Node in
//the linked list. It first determines if the list is empty then it determines
//if the string exists within the list.
//----------------------------------------------------------------------------
void List::loadNode(string str)
{
      Node *newNode,*nodePtr, *previousNode;
      
            newNode=createNode(str);
          
      

      //If there are no nodes in the list or new node is smaller
      //than the first node then
      //make the new node the first node
      if (!head||(strcmpi(str.c_str(),head->word.c_str())<0))
      {                
            
            newNode->nextPtr=head;
          head=newNode;
      }
      
      
            //otherwise, insert the new node
            //at the correct place in the list
      
      else
      {
            //Initiliaze nodePtr to head of the list
            nodePtr= head;
            
      
            while(nodePtr) //&&(!strcmpi(nodePtr->word.c_str(),str.c_str())<0))
            {  
                  
                  if(strcmpi(nodePtr->word.c_str(),str.c_str())==0)
                  {
                        
                  nodePtr->wordCount++;
                  return;
                  }
              else
                  {  
                     previousNode=nodePtr;
               nodePtr=nodePtr->nextPtr;
                  }
            }
              //Insert the new Node as the last node
      previousNode->nextPtr=newNode;
      newNode->nextPtr=nodePtr;
      }
   

   
}
//---------------------------------------------------------------------------
//Function DisplayFile displays each character of the input file
//---------------------------------------------------------------------------
void DisplayFile()
{
            ifstream ifile;

if (!openfile(ifile,"inputfile.txt"))
{
      cout<<"Error opening inputfile.txt "<<endl;
      exit(1);
}
      
      
      char ch;
       while( ifile.get(ch) )
      {
          cout << ch;
      
   }
       cout<<endl;
      ifile.close();
      cout<<endl;
}

//----------------------------------------------------------------------------
//Function findMax searches for the highest value of wordCount
//it then dispays the corresponding words of max occurances.
//----------------------------------------------------------------------------


void List::findMax()
{



Node *nodePtr;
nodePtr=head;
int max=1;
while(nodePtr)       //searches for highest value of wordCount
      
      {
            if(max>=nodePtr->wordCount)
            {
                  
               
            nodePtr=nodePtr->nextPtr;
            }
      else
      {
            max=nodePtr->wordCount;
         nodePtr=nodePtr->nextPtr;
      }
      }

nodePtr=head; //reset nodePtr to head


cout<<"The most used words occuring "<<max<<" times are:"<<endl;

while(nodePtr)    //Find all values equal to max
{
   if (nodePtr->wordCount==max)
   {
         cout<<nodePtr->word<<"    ";

   }

   nodePtr=nodePtr->nextPtr;

}

cout<<endl;
cout<<endl;


}

//-----------------------------------
//Destructor deletes nodes
//-----------------------------------
List::~List
{
      Node *nodePtr, *nextNode;

nodePtr=head;
while(nodePtr!=NULL)
{
      nextNode=nodePtr->next;
      delete nodePtr;
      nodePtr=nextNode;

}

0
Comment
Question by:Stella Pauley
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
5 Comments
 
LVL 86

Expert Comment

by:jkr
ID: 10882042
This is a commpn indication that you have

a) not the latest service pack
b) not enough comment lines in your code (and yes, I am serious)

Regarding a)

Get either

http://msdn.microsoft.com/vstudio/downloads/updates/sp/vs6/sp5/default.aspx (SP5, well tested)

or

http://msdn.microsoft.com/vstudio/downloads/updates/sp/vs6/sp6/default.aspx (SP6, quite new)

Regarding b)

These "internal compiler errors" are hard to tackle, but seriously, inserting or removing lines before the line that caused that problems helps in some cases :o)

0
 
LVL 1

Author Comment

by:Stella Pauley
ID: 10882310
I have service pack 6 installed...

0
 
LVL 86

Accepted Solution

by:
jkr earned 2000 total points
ID: 10882381
Then, try to "Rebuild all". Oh, and make the destructor read

List::~List() // <---- the brackets were missing :-(
{
    Node *nodePtr, *nextNode;

nodePtr=head;
while(nodePtr!=NULL)
{
    nextNode=nodePtr->next;
    delete nodePtr;
    nodePtr=nextNode;

}
0
 
LVL 1

Author Comment

by:Stella Pauley
ID: 10882416
You hit it on the mark.
I had forgot the parameter parathensis ( ) at the end of the Destructor in the definition.


0
 
LVL 86

Expert Comment

by:jkr
ID: 10882435
>>You hit it on the mark.

Should have seen that *way* earlier :-(
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
Suggested Courses

649 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question