2015-12-31 52 views
0

他們給了我在我的測試文件中的以下代碼來實現:如何調用對象/構造函數返回一個二維數組

cout << "Testing the Matrix constructors:" << endl; 

cout << "Case 1: Creating a 2x4 matrix of zeros with the standard constructor:" << endl; 
{ 
    Matrix matrix(2, 4); 
    cout << matrix << endl; 

目前我在我的.cpp文件中的代碼爲構造函數是如下:

Matrix::Matrix (const int noOfRows, const int noOfCols){ 

double **p_matrix = new double*[noOfRows]; 
for(int i=0; i< noOfRows; i++){ 
    p_matrix[i] = new double[noOfCols]; 
} 

for(int i=0; i< noOfRows; i++){ 
    for(int j=0; j<noOfCols; j++){ 
     p_matrix[i][j] = 0; 
    } 
} 

我的主要困惑是因爲我可以我的構造函數中打印出2x4的我的矩陣,而不需要這行代碼的COUT < <矩陣的一部分。但是,我被要求包括矩陣,並且我不確定我理解它是如何工作的。它是否調用我的對象矩陣?如果是這樣,我怎樣才能返回我的二維數組p_matrix,因爲我無法從構造函數中返回值?

我想一個解決方案可能是超載我< <操作,如下圖所示:

std::ostream& operator<<(std::ostream& output, const Matrix& rhs){ 
output << rhs.data << std::endl; 
return output; } 

我把rhs.data的原因是因爲我想rhs.matrix和rhs.p_matrix卻得到了一個錯誤,需要一個成員變量。在我的.h文件中,是我唯一允許的成員變量如下:

  1. INT noOfRows:存儲數量的成員變量:存儲行
  2. INT noOfColumns數量的成員變量列
  3. 雙*數據:存儲將地址矩陣項的1-d陣列排列逐列,即第一柱,然後通過第2列和等等等等 所述
  4. INT成員變量GetIndex(const int rowIdx,const int columnIdx)const:成員 函數,確定posi沿着由rowIdx指定的行中的矩陣條目的1-D數組(數據)和由columnIdx指定的列的變化(索引)。

我不確定如何使用運算符重載只使用這些變量,所以這是最好的解決方案還是有其他方法嗎?考慮到我不能改變我的測試文件中的侷限性或4成員變量

回答

1

至於你說:

在我的.h文件中,唯一的成員變量我被允許是...雙*數據:存儲地址矩陣的1-d陣列

所以,Matrix構造應該初始化data屬性,而不是本地double **p_matrix變量(然後離開data未初始化)的成員變量...

只需更換:

Matrix::Matrix (const int noOfRows, const int noOfCols) 
{ 
    double **p_matrix = new double*[noOfRows]; 
    for(int i=0; i< noOfRows; i++){ 
     p_matrix[i] = new double[noOfCols]; 
    } 

    for(int i=0; i< noOfRows; i++){ 
     for(int j=0; j<noOfCols; j++){ 
      p_matrix[i][j] = 0; 
     } 
    } 
} 

通過:

1。如果您data屬性是double**

Matrix::Matrix (const int noOfRows, const int noOfCols) 
{ 

    this->noOfRows = noOfRows; 
    this->noOfCols = noOfCols; 

    data = new double*[noOfRows]; 
    for(int i=0; i< noOfRows; i++){ 
     data[i] = new double[noOfCols]; 
    } 

    for(int i=0; i< noOfRows; i++){ 
     for(int j=0; j<noOfCols; j++){ 
      data[i][j] = 0; 
     } 
    } 
} 

後來,你可以這樣做:

std::ostream& operator<<(std::ostream& output, const Matrix& rhs) 
{ 
    for(int i=0; i< noOfRows; i++){ 
     for(int j=0; j < noOfCols; j++){ 
      output << rhs.data[i][j] << " "; // next column 
     } 
     output << std::endl; // next line 
    } 
    return output; 
} 

2.如果您的data屬性爲double*

Matrix::Matrix (const int noOfRows, const int noOfCols){ 

    this->noOfRows = noOfRows; 
    this->noOfCols = noOfCols; 

    data = new double[noOfRows*noOfCols]; 
    for(int i=0; i< noOfRows*noOfCols; i++){ 
     data[i] = 0; 
    } 
} 

後來,你可以做:

std::ostream& operator<<(std::ostream& output, const Matrix& rhs) 
{ 
    for(int i=0; i< noOfRows; i++){ 
     for(int j=0; j < noOfCols; j++){ 
      output << rhs.data[noOfCols*i+j] << " "; // next column 
     } 
     output << std::endl; // next line 
    } 
    return output; 
} 

在這兩種情況下,確保data在你的頭文件是公開的,或者使operator<<(std::ostream& output, const Matrix& rhs)Matrixfriend(或添加一個getter)。

順便說一下,請注意矩陣通常存儲爲double*而不是double**

+0

我仍然收到一個錯誤「無法將'double **'轉換爲'double *'在賦值中」,因爲我需要將數據初始化爲double *,所以這就是爲什麼我創建了p_matrix。有沒有辦法去引用我的雙** p_matrix並使其對應於double *數據? – MachoSkinny

+0

沒辦法,你不能把'double **'轉換成'double *'。我編輯我的帖子,以顯示你應該如何做,具體取決於如果你的屬性是一個'double *'或一個'雙**' – jpo38

+0

謝謝,我有數據部分工作,但noOfRows和noOfCols不能正常工作在操作員「我已經成爲Matrix的朋友。當我做rhs.noOfRows,我認爲它是從我的頭文件中未定義的原始值。當我簡單地使用noOfRows時,它說它不在這個範圍內聲明,即使這個操作符是一個朋友,並且在它被用作我的構造函數的輸入之後出現。在我的構造函數中是否需要額外的行來匹配我的頭文件函數中的原始noOfRows變量和輸入到我的構造函數中? – MachoSkinny

相關問題