2017-06-15 13 views
-5

コードはコンパイルされていますが、コンソールウィンドウは消えます。 print()関数に何か問題がありますか、コンパイラ(Dev C++)ですか? main()関数でさまざまな方法で印刷しようとしましたが、このコードが動作している間にエラーが発生するものがあります。2D Safe Array C++

あなたは外部プロセスを呼び出すことによって、あなたのプロセスを一時停止しようとしている
#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; 
} 
+0

押さえてください。 –

+0

この配列は、newが代入演算子で例外をスローする場合、もはや "安全"ではありません。そしてなぜコンストラクタへのパラメータが間違っているだけで 'exit()'を呼び出すことによってアプリケーション全体を終了していますか? – PaulMcKenzie

+0

'std :: vector'がこの割り当てに許可されていないと安全に仮定できますか? – user4581301

答えて

0

、その呼び出しはところで、失敗します。独自のプロセスを一時停止する関数呼び出しを使用する必要があります。

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

[編集]あなたの行列Aは2×2であり、あなたの指標の一つは、範囲外です。

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

使いやすいデバッガでより強力なIDEを使用することを検討する必要がありますか?実際の生産環境で使用されている多くの優れたフリー・ソリューションがあります。ここでは名前は言及したくありませんが、プロの設定で最も広く使用されているIDEは、非常に優れた統合デバッガで無料で利用できます。