-5

The code is compiling, but the console window disappears. Is there something wrong in the print() funtion or is is the compiler(Dev C++)? I tried printing in the main() function in many different ways but some of them give me errors while this code works.

#include<iostream>
#include<cstdlib> 
#include<cassert> 
using namespace std;

const int ROW = 2;
const int COL = 2;

template<class T> class SA
{
private:
    int low, high;
    T* p;
public:
    // default constructor
    SA()
    {
        low = 0;
        high = -1;
        p = NULL;
    }
    // 2 parameter constructor lets us write 
    SA(int l, int h)
    {
        if ((h - l + 1) <= 0)
        {
            cout << "constructor error in bounds definition" << endl;
            exit(1);
        }
        low = l;
        high = h;
        p = new T[h - l + 1];
    }

    // single parameter constructor lets us
    // SA x(10); and getting an array x indexed from 0 to 9
    SA(int i)
    {
        low = 0;
        high = i - 1;
        p = new T[i];
    }

    // copy constructor for pass by value and 
    // initialization
    SA(const SA & s)
    {
        int size = s.high - s.low + 1;
        p = new T[size];

        for (int i = 0; i < size; i++)
            p[i] = s.p[i];
        low = s.low;
        high = s.high;
    }

    // destructor
    ~SA()
    {
        delete[] p;
    }

    //overloaded [] lets us write 
    //SA x(10,20); x[15]= 100;
    T& operator[](int i)
    {
        if (i < low || i > high)
        {
            cout << "index " << i << " out of range" << endl;
            exit(1);
        }
        return p[i - low];
    }

    // overloaded assignment lets us assign one SA to another
    SA & operator=(const SA & s)
    {
        if (this == &s)
            return *this;
        delete[] p;
        int size = s.high - s.low + 1;
        p = new T[size];

        for (int i = 0; i < size; i++)
            p[i] = s.p[i];
        low = s.low;
        high = s.high;
        return *this;
    }
    // overloads << so we can directly print SAs 
    friend ostream& operator<<(ostream& os, SA s)
    {
        int size = s.high - s.low + 1;
        for (int i = 0; i < size; i++)
            cout << s.p[i] << endl;
        return os;
    };
    //end of ostream
};
//end class of safeArray 

//Matrix class
template<class T> class Matrix
{
private:

    SA<SA<T> > matrx;
public:
    //2 param for 2D array
    Matrix(int r, int c)
    {
        matrx = SA<SA<T> >(0, r - 1);
        for (int i = 0; i < r; i++)
        {
            matrx[i] = SA<T>(0, c - 1);
        }
    }

    SA<T> operator[](int row)
    {
        return (matrx[row]);
    }

    void read()
    {
        for (int i = 0; i < ROW; i++)
            for (int j = 0; j < COL; j++)
                cin >> this->s[i][j];
    }

    void print()
    {
        for (int i = 0; i < ROW; i++)
            for (int j = 0; j < COL; j++)
                cout << this->s[i][j] << "\t" << endl;
    }
};
//class Matrix

int main()
{

    Matrix<int> A(2, 2);

    A[0][2] = 5;
    cout << A[0][2];
    //A.print();
    /*
     Matrix <int> A;
     cout<<"Enter matrix A:"<<endl;
     A.read();
     cout<<"Matrix A is: "<<endl;
     A.print();
     */

    system("PAUSE");
    return 0;
}
Sneftel
  • 40,271
  • 12
  • 71
  • 104
  • Please, indentation. – Hatted Rooster Jun 15 '17 at 17:08
  • This array is no longer "Safe" if `new` throws an exception in the assignment operator. And why are you exiting the entire app by calling `exit()` just because the parameters to the constructor are incorrect? – PaulMcKenzie Jun 15 '17 at 17:19
  • Can we safely assume `std::vector` is not allowed for this assignment? – user4581301 Jun 15 '17 at 17:34
  • Good job on taking the Rule of Three into account. A lot of people miss it. If you want, you can simplify your assignment operator with the [Copy and Swap Idiom](https://stackoverflow.com/questions/3279543/what-is-the-copy-and-swap-idiom). – user4581301 Jun 15 '17 at 17:39
  • `Matrix::operator[]` makes a copy. Not sure you want to do that. – user4581301 Jun 15 '17 at 17:42
  • `A[0][2]` on a 2x2 array is out of bounds. If you wanted to test that the bounds check works, you did it. Better if you throw an exception though. You can catch the exception and print out "Successfully trapped an out of bounds access!" which is a bit more useful than ending the program with the headsman's axe. – user4581301 Jun 15 '17 at 17:46
  • What exactly are we looking for, anyway? – user4581301 Jun 15 '17 at 17:47

1 Answers1

0

You are you trying to pause your process by calling an external process, that call fails, BTW. You should use a function call that pauses your own process.

system("PAUSE");  // this is wrong.
getch();          // this will wait for the user to press a key.

[edit] Your matrix A is 2x2 and one of your indices is out of bounds.

 A[0][2] = 5; // out of bounds !! valid bounds are [0..(2-1 = 1)][0..1]

You should consider using a more powerful IDE with an easier-to-use debugger? There are many good free solutions out there, that are used in real-world production environments. I don't want to mention any names here, but the most widely used IDEs in professional settings are available for free, with very good integrated debuggers.

Michaël Roy
  • 6,338
  • 1
  • 15
  • 19