post #1 of 1
Thread Starter 
I'm stuck on this assignment which deals with adding, assigning, comparing objects...using operator overloading...i.e.(a = b; a = b + c; a == b


I'm getting a "debug assertion failed" error...which I'm assuming is improper use of deallocating memory...or something to do with my copy constructor/assignment operator function. Any help would be appreciated.



Code:
//IntArray.h

#ifndef _IntArray_H
#define _IntArray_H

const int default1 = 10;

class IntArray{

private:
char *name;
int small, big;
int *array1;
int randomint;

public:

IntArray();
IntArray(int num);
IntArray(int num, int num2);
IntArray(const IntArray& c);

void setName(const char* name);
int low() const;
int high() const;
IntArray& operator+(const IntArray &s);
IntArray& operator=(const IntArray &s);
void operator+=(const IntArray &s);
int operator==(const IntArray &s);
int operator!=(const IntArray &s);

int operator[](int i) const;
int& operator[](int i);

friend std::ostream& operator<<(std::ostream& os, const IntArray& c);

~IntArray(){
delete [] name;
delete [] array1;
}
}; 

#endif


Code:
//IntArray.cpp

#include <iostream>
#include <fstream>
#include "IntArray.h"

using namespace std;

extern ofstream csis;


IntArray::IntArray(){

array1 = new int[default1];
big = default1 - 1;
small = 0;
name = '\\0';
randomint = 1;
}

IntArray::IntArray(int num){

array1 = new int[num];
big = num - 1;
small = 0;
name = '\\0';
randomint = 1;
}

IntArray::IntArray(int num, int num2){
randomint = 1;
if(num > num2){
cout << "Run time error: illegal indices" << endl;
}
else{
array1 = new int[num2 - num + 1];
big = num2;
small = num;
}
}

IntArray::IntArray(const IntArray& c){
randomint = 1;
*this = c;
}

void IntArray::setName(const char* name){

size_t len = 0;
if (name)
{
len = strlen(name);
this->name = new char[len + 1];
strcpy(this->name, name);
this->name[len] = '\\0';
}
}

int IntArray::low( ) const{

return small;
}

int IntArray::high( ) const{

return big;
}

IntArray& IntArray::operator+(const IntArray &s){

big += s.big;
small += s.small;
return *this;
}

IntArray& IntArray::operator=(const IntArray &s){

if(this == &s)
       return *this;
array1 = new int[s.big + s.small + 1];
big = s.big;
small = s.small;
return *this;
}

void IntArray::operator+=(const IntArray &s){

big += s.big;
small += s.small;
}

int IntArray::operator==(const IntArray &s){
if((s.big - s.small) == (big - small))
return 1;
else
return 0;
}

int IntArray::operator!=(const IntArray &s){
if((s.big - s.small) == (big - small))
return 0;
else
return 1;
}

int IntArray::operator[](int i) const{
if(i < small || i > big){
cout << "subscript out of bounds" << endl;
return randomint;
}
else
return array1[i];
}

int& IntArray::operator[](int i){
if(i < small || i > big){
cout << "subscript out of bounds" << endl;
return randomint;
}
else
return array1[i];
}

std::ostream& operator<<(std::ostream& os, const IntArray& s){

for(int i = s.small; i <= s.big; ++i){
os << s.name << "[" << i << "]" << "=" << s.array1[i] << endl;  
}
return os;
}

portion of code in main for one test
Code:
void test3() {

    cout << "3. Array declared with two integers: IntArray c(6, 8);" << endl << endl;

    csis << "3. Array declared with two integers: IntArray c(6, 8);" << endl << endl;

    IntArray c(6, 8);

    for(int i = c.low(); i <= c.high(); i++)

        c[i] = i * 10;

    c.setName("c");

    cout << c << endl;

    csis << c << endl;

    wait();

}

Edited by surfbumb - 3/22/11 at 10:59pm
Black Silence
(15 items)
 
  
CPUMotherboardRAMHard Drive
i5 3570k @ 4.5 GHz Asus P8Z77-M Pro Kingston HyperX Genesis 8 GB - 1600 MHz Seagate Barracuda 250 GB 
Optical DriveCoolingOSMonitor
Samsung WriteMaster Noctua NH-D14 Windows 10 ASUS VS24AH-P 
KeyboardPowerCaseMouse
Logitech Navigator Enermax Infiniti 650W Fractal R3 Black Pearl Razer Death Adder 
Mouse PadAudio
SteelSeries QcK Mass Altec Lansing FX4021 
  hide details  
Reply
Black Silence
(15 items)
 
  
CPUMotherboardRAMHard Drive
i5 3570k @ 4.5 GHz Asus P8Z77-M Pro Kingston HyperX Genesis 8 GB - 1600 MHz Seagate Barracuda 250 GB 
Optical DriveCoolingOSMonitor
Samsung WriteMaster Noctua NH-D14 Windows 10 ASUS VS24AH-P 
KeyboardPowerCaseMouse
Logitech Navigator Enermax Infiniti 650W Fractal R3 Black Pearl Razer Death Adder 
Mouse PadAudio
SteelSeries QcK Mass Altec Lansing FX4021 
  hide details  
Reply