Solved

Destructor error

Posted on 2004-04-21
5
454 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
  • 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 500 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

919 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now