Get an Error as soon as the program runs.

This is a discussion on Get an Error as soon as the program runs. within the C++ Programming forums, part of the General Programming Boards category; Hi, I am getting an error as soon as I run my program. The error is main.exe has caused a ...

  1. #1
    Registered User
    Join Date
    Feb 2006
    Posts
    40

    Get an Error as soon as the program runs.

    Hi,
    I am getting an error as soon as I run my program. The error is main.exe has caused a system error and needs to be closed. I know there is something wrong with my Container class but I can't find out the problem.

    My Container class
    Code:
    #include <iostream.h>  
    
    //file SetOfPersons.h 
    class SetOfPersons { 
    public: 
    	SetOfPersons(int initial_size = 4) {
        //default constructor allocate appropriate heap storage 
        //store elements on heap array declared like this: 
        //new Person*[initial_size]; 
    
         cout << "SetOfPersons(int)\n";
                    numberOfElements = initial_size;
                    elements = new  Person*[initial_size];}
    
    
    
    	~SetOfPersons(void) {delete [] elements;} 
    
    
        void add(Person & element) { 
             if(capacity == size()) {  //grow the container 
                cout << "growing\n"; 
                Person **temp = elements; 
                *elements = new Person[capacity*2]; 
                for(int i=0; i<capacity; i++) *elements[i] = *temp[i]; 
                numberOfElements = numberOfElements *2; 
                delete [] *temp; 
                } 
          *elements[capacity++] = element; 
         } 
    	Person & someElement(){ 
    		capacity++; 
            capacity = capacity % numberOfElements; 
            return *elements[capacity]; }
    
    	Person & removeSomeElement() {
    	// answer some (any) element from the set and remove it from the set
        // if the set is more than half empty release some memory
          
             capacity--; 
             if(capacity < numberOfElements/2) {  //shrink the container 
                cout << "shrinking\n"; 
                Person ** temp = elements; 
                *elements = new Person[numberOfElements/2]; 
                for(int i=0; i<capacity; i++) elements[i] = temp[i]; 
                numberOfElements = numberOfElements/2; 
                delete [] temp; 
             } 
          return *elements[capacity];
         }
    
    
    	int size(){
    		//answer the number of elements in the set. 
    		//not just the size of the array
    		return numberOfElements;
    	}
    	
    	void printOn(ostream & ostr){
    	int i ;
    	for( i = 0; i<size(); i++) { 
    		cout<<*elements[i]<<"\n";
        } 
      
    	}
    
    private: 
    	Person ** elements; 
    	int numberOfElements; //number of elements in the set 
    	int capacity; //size of the available array memory 
    }; 
    
    //you will need the following global operator to print Sets. 
    //Put this in the same file as class Set, but not inside the Set class. 
    ostream & operator<<(ostream & ostr, SetOfPersons & s) { 
         s.printOn(ostr); 
         return ostr; 
    }

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,417
    Well you don't do a very good job of initialising or updating capacity at appropriate times.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User
    Join Date
    Feb 2006
    Posts
    40
    thanks. Can you point me where should I make the changes.

  4. #4
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,417
    Well like your constructor should have
    capacity = 0;
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  5. #5
    Registered User
    Join Date
    Feb 2006
    Posts
    40
    you mean like this:
    Code:
    SetOfPersons(int initial_size = 4) {
        //default constructor allocate appropriate heap storage 
        //store elements on heap array declared like this: 
        //new Person*[initial_size]; 
        capacity = 0;
         cout << "SetOfPersons(int)\n";
                    numberOfElements = initial_size;
                    elements = new  Person*[initial_size];}
    I still have the same problem.
    Last edited by sara.stanley; 05-28-2006 at 03:41 PM.

  6. #6
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,417
    Well carry on with the analysis into the next function - it's your code, you debug it.

    Run the code inside a debugger and step through it line by line, and examine the variables affected by each statement.
    When it does something you didn't expect, you've found a bug.

    delete [] *temp;
    delete [] temp;
    Which one of these is correct?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. BOOKKEEPING PROGRAM, need help!
    By yabud in forum C Programming
    Replies: 3
    Last Post: 11-16-2006, 10:17 PM
  2. Can someome help me with a program please?
    By WinterInChicago in forum C++ Programming
    Replies: 3
    Last Post: 09-21-2006, 10:58 PM
  3. Need help with my program...
    By Noah in forum C Programming
    Replies: 2
    Last Post: 03-11-2006, 06:49 PM
  4. I need some help with my program please.
    By agentxx04 in forum C Programming
    Replies: 9
    Last Post: 09-26-2004, 07:51 AM
  5. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 10:04 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21