2017-03-12 9 views
0

加算演算子をオーバーロードしようとしていますが、メモリが割り当て解除された後に値で引き数を渡していてもセグメンテーションフォルトが発生し続けます。誰も私が間違っていることが何であるか考えている。また、オーバーロードされた追加を修正した後、これを正しく実行すると、配列を指す代わりにベクターを使用する必要があります。これは、配列に対して何らかの形で宣言する方法がわかりません。加算演算子と動的ベクトルをオーバーロードするときのセグメンテーションフォールト

RowAray.h

#ifndef ROWARAY_H // if constant ROWARAY_H not defined do not execute 
#define ROWARAY_H // defines constant ROWARAY_H 
#include <new>  // Needed for bad_alloc exception 
#include <cstdlib> // Needed for the exit function 


template <class T> 
class RowAray{ 
    private: 
     int size; 
     T *rowData; 
     void memError(); // Handles memory allocation errors 
     void subError(); // Handles subscripts out of range 
    public: 
     RowAray(T); //used to construct row Array object 
     ~RowAray(){delete [] rowData;} //used to deallocate dynamically allocated memory from Row array 
     int getSize(){return size;} //inline accessor member function used to return length of Row array 
     void setData(int row, T value); 
     T getData(int i){return ((i >=0&& i < size)?rowData[i]:0);} // 
     T &operator[](const int &); 
}; 
template <class T> 
RowAray<T>::RowAray(T colSize){ 
    size =colSize>1?colSize:1; 
    // Allocate memory for the array. 
    try 
    { 
     rowData = new T [size]; 
    } 
    catch (bad_alloc) 
    { 
     memError(); 
    } 

    // Initialize the array. 
    for (int count = 0; count < size; count++){ 
     T value = rand()%90+10; 
     setData(count, value); 
    } 

} 

template <class T> 
void RowAray<T>::memError() 
{ 
    cout << "ERROR:Cannot allocate memory.\n"; 
    exit(EXIT_FAILURE); 
} 

template <class T> 
void RowAray<T>::subError() 
{ 
    cout << "ERROR: Subscript out of range.\n"; 
    exit(EXIT_FAILURE); 
} 

template <class T> 
T &RowAray<T>::operator[](const int &sub) 
{ 
    if (sub < 0 || sub >= size) 
     subError(); 
    else 
     return rowData[sub]; 
} 

template <class T> 
void RowAray<T>::setData(int row, T value){ 
    //used to fill array with random 2 digit #s 
     *(rowData + row) = value; 
} 

#endif /* ROWARAY_H */ 

Table.h

#ifndef TABLE_H 
#define TABLE_H 

#include "RowAray.h" 

template <class T> 
class Table{ 
    private: 
     int szRow; 
     int szCol; 
     RowAray<T> **records; 

    public: 
     Table(int,int); //used to construct Table object 
     Table(const Table &); 
     ~Table(); //used to deallocate dynamically allocated memory from Table object 
     int getSzRow() const{return szRow;} //used to return row size 
     int getSzCol()const {return szCol;} 
     Table operator+(const Table &); 
     T getRec(int, int) const; //used to return inserted random numbers of 2d arrays 
}; 

template <class T> 
Table<T>::Table(int r, int c){ 
    //Set the row size 
    this->szRow = r; 
    //Declare the record array 
    records = new RowAray<T>*[this->szRow]; 
    //Size each row 
    this->szCol = c; 
    //Create the record arrays 
    for(int i=0;i<this->szRow;i++){ 
     records[i]=new RowAray<T>(this->szCol); 
    } 
} 

template <class T> 
Table<T>::Table(const Table &Tab){ 
    szRow=Tab.getSzRow(); 
    szCol=Tab.getSzCol(); 
    records = new RowAray<T>*[szCol]; 

    for(int i = 0; i < this->szCol; i++){ 
     records[i] = new RowAray<T>(szRow); 
    } 

    //set elements = to random value 
    for(int row = 0; row < szRow; row++){ 
     for(int col = 0; col < this->szCol; col++){ 
      int value = Tab.getRec(row, col); 
      records[col]->setData(row,value); 
     } 
    } 


} 

template <class T> 
T Table<T>::getRec(int row, int col) const{ 
    //if else statement used to return randomly generated numbers of array 
    if(row >= 0 && row < this->szRow && col >= 0 && col < this->szCol){ 
     return records[row]->getData(row); 
    }else{ 
     return 0; 
    } 
} 

template <class T> 
Table<T>::~Table(){ 
    //Delete each record 
    for(int i=0;i<this->szRow;i++){ 
     delete records[i]; 
    } 
    delete []records; 
} 

template <class T> 
Table<T> Table<T>::operator+(const Table &Tab){ 
    Table temp(Tab.getSzRow(), Tab.getSzCol()); 

    //set elements = to random value for operation to 
    for(int row=0; row < szRow; row++){ 
     for(int col=0; col < szCol; col++){ 
      int value = getRec(row, col) + Tab.getRec(row, col); 
      temp.records[col]->setData(row,value); 
     } 
    } 
    return temp; 
} 

#endif /* TABLE_H */ 

main.cppに

#include <cstdlib> 
#include <ctime> 
#include <iostream> 
#include <iomanip> 
using namespace std; 

//User Libraries 
#include "Table.h" 

//Global Constants 

//Function Prototype 
template<class T> 
void prntRow(T *,int); 
template<class T> 
void prntTab(const Table<T> &); 

//Execution Begins Here! 
int main(int argc, char** argv) { 
    //Initialize the random seed 
    srand(static_cast<unsigned int>(time(0))); 

    //Declare Variables 
    int rows=3,cols=4; 

    //Test out the Row with integers and floats 
    RowAray<int> a(3); 
    RowAray<float> b(4); 
    cout<<"Test the Integer Row "<<endl; 
    prntRow(&a,3); 
    cout<<"Test the Float Row "<<endl; 
    prntRow(&b,4); 

    //Test out the Table with a float 
    Table<float> tab1(rows,cols); 
    Table<float> tab2(tab1); 
    Table<float> tab3 = tab1 + tab2; 

    cout<<"Float Table 3 size is [row,col] = Table 1 + Table 2 [" 
      <<rows<<","<<cols<<"]"; 
    prntTab(tab3); 

    //Exit Stage Right 
    return 0; 
} 

template<class T> 
void prntRow(T *a,int perLine){ 
    cout<<fixed<<setprecision(1)<<showpoint<<endl; 
    for(int i=0;i<a->getSize();i++){ 
     cout<<a->getData(i)<<" "; 
     if(i%perLine==(perLine-1))cout<<endl; 
    } 
    cout<<endl; 
} 

template<class T> 
void prntTab(const Table<T> &a){ 
    cout<<fixed<<setprecision(1)<<showpoint<<endl; 
    for(int row=0;row<a.getSzRow();row++){ 
     for(int col=0;col<a.getSzCol();col++){ 
      cout<<setw(8)<<a.getRec(row,col); 
     } 
     cout<<endl; 
    } 
    cout<<endl; 
} 

答えて

0

あなたのエラーがTable.hのライン85から茎、あなたの中に+演算子のオーバーロード:

temp.records[col]->setData(row,value); 

は本当に

別に
temp.records[row]->setData(col,value); 

する必要がありますその62行目の同じファイルに別のエラーが見つかりました(方法Table<T>::getRec(int row, int col)):

return records[row]->getData(row); 

は別に、これらの問題から

return records[row]->getData(col); 

する必要があり、私は非常に多くの一部が、(多分あなたはまた、コードレビューを試みることができる)あなたのコードを再考し、再構築するためにあなたを助言しますそれは私の意見では不必要で、プロジェクトを拡張するといくつかの問題が発生する可能性があります(例えば、RowArayのコンストラクタはT colSizeを引数としてとり、int、あるいはより良いものはsize_tです)。 Tが暗黙的に整数型に変換できない場合、このコードはそのまま動作しません。

+0

ありがとうございました!出来た。これらの行がセグメンテーション違反を引き起こした理由を説明できますか?また、私は宿題のためにこの構造に従わなければなりません。私はそれのいくつかが無意味であることを理解していますが、私はファイル構造に従わなければなりません。 – EggplantMachina

+0

@EggplantMachina確かに。あなたのテーブルは3行4列です。反復子変数 'col'が最大値に達すると、' temp.records [col] 'は' temp.records [3] 'と同じです。しかし、 'temp.records'は' temp.getSzRow() 'エントリしか保持しないので、3番目の要素へのアクセスは範囲外です。 –

関連する問題