2D安全阵列C ++

时间:2017-06-15 17:07:57

标签: c++

代码正在编译,但控制台窗口消失了。 print()函数有什么问题,还是编译器(Dev C ++)? 我尝试在main()函数中以许多不同的方式进行打印,但是当这些代码有效时,其中一些会给我错误。

#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;
}

1 个答案:

答案 0 :(得分:0)

您是在试图通过调用外部进程来暂停您的进程,该呼叫失败,BTW。您应该使用暂停自己进程的函数调用。

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

[edit]您的矩阵A是2x2,其中一个索引超出范围。

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

您应该考虑使用功能更强大的IDE和更易于使用的调试器吗?有许多优秀的免费解决方案,用于现实世界的生产环境。我不想在这里提到任何名称,但在专业设置中使用最广泛的IDE是免费提供的,具有非常好的集成调试器。