Solved

Destructor error

Posted on 2004-04-21
5
453 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
Comment Utility
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
Comment Utility
I have service pack 6 installed...

0
 
LVL 86

Accepted Solution

by:
jkr earned 500 total points
Comment Utility
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
Comment Utility
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
Comment Utility
>>You hit it on the mark.

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

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
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 goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

744 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

17 Experts available now in Live!

Get 1:1 Help Now