2016-04-30 14 views
1

私のコードは[n] x [n]行列に整数を格納します。しかし、printMatrix()メソッドを使用すると、以前にマトリックスに割り当てた整数ではなく、コマンドプロンプトでゼロだけが返されます。私は参照/ポインタを使用する必要があると思うが、私はどのようにはっきりしていない。ここに私のコードです:クラスの2番目の関数が最初のものにアクセスできない

#include <iostream> 
#include <vector> 

using namespace std; 

class Matrices 
{ 
public: 
    Matrices(); 
    void storeMatrix(vector <vector <int> > matr, int n); 
    void printMatrix(vector <vector <int> > matr, int n); 

private: 
    int n; 
    vector <vector <int> > matr; 
    int cell; 
};//class Matrices 
Matrices::Matrices() 
{ 

}//Constructor 

void Matrices::storeMatrix(vector <vector <int> > matr, int n) 
{ 
    for(int i = 0; i < n; i++) 
     { 
      for(int j = 0; j < n; j++) 
      { 
       cout << "Please insert an int for matr [" << i << "] [" << j << "]" << endl; 
       cin >> cell; 
       matr[i][j] = cell; 
      } 
     } 


}//storeMatrix 

void Matrices::printMatrix(vector <vector <int> > matr, int n) 
{ 
    for(int i = 0; i < n; i++) 
     { 
      for(int j = 0; j < n; j++) 
      { 
       cout << matr[i][j]; 
      } 
      cout<<endl; 
     } 
}//printMatrix 


int main() 
{ 
    int m; 
    cin >> m; 
    int n = 1; 

    for (int i = 1; i <= m; i++) // loop to make the input integer equivalent to power of 2; 
     n = n * 2; 

    vector <vector <int> > matr (n , vector <int> (n)); 
    Matrices k; 
    k.storeMatrix(matr, n); 
    k.printMatrix(matr, n); 


    return 0; 
} 

どのようにそれを修正するためのアイデア?

+0

あなたはstoreMatrix' 'のための参照を必要とする程度ですね。 'printMatrix'では、必要ではないはずですが、const refはパフォーマンスを節約できます。さらに、あなたのデザインは少し不明です。 'printMatrix'は、格納された行列を出力するか、渡された行列を出力しますか?なぜ、関数のパラメータは、クラスのパラメータと同じ名前ですか? – AndyG

+0

printMatrixはstoreMatrixに格納された行列を出力する必要があります – Celestiny

答えて

1

printMatrix()に関連するすべてのパラメータを削除します。 main()とそのプロトタイプ(クラス内)とその定義void YourClassName::printMatrix()から。

cout << this->matr[i][j]; //更なる説明については、以下の

の代わりに、この参照:This」のプライベートメンバにこの現在のインスタンスを印刷します追加

cout << matr[i][j];

をして、その定義内でこれを設定 "と入力してください。

また、store()には、現在のインスタンスで行っていることを「リンク」する必要があります。

全体:このデザインは、何が起こっているかをよりよく理解するのに役立ちます。

int main() 
{ 
    int m; 
    int n = 1; 

    cin >> m; 

    Matrices k(m, n); // create an object k 
    k.storeMatrix(); // store matrix for "this" object k 
    k.printMatrix(); // print matrix of "this" object k 

    return 0; 
} 

をし、クラスを見て:このメイン()を参照してください

class Matrices 
{ 
public: 
    Matrices(); 
    Matrices(int, int); 
    void storeMatrix(); 
    void printMatrix(); 

private: 
    int n; 
    vector <vector <int> > matr; 
    int cell; 
};//class Matrices 

Matrices::Matrices() 
{} 

Matrices::Matrices(int m, int n) 
{ 
    for (int i = 1; i <= m; i++) // loop to make the input integer equivalent to power of 2; 
     n = n * 2; 

    vector <vector <int> > _matr (n, vector <int>(n)); 
    this->matr = _matr; 

}//Constructor 

void Matrices::storeMatrix() 
{ 
    for (int i = 0; i < this->matr.size(); i++) 
    { 
     for (int j = 0; j < this->matr[i].size(); j++) 
     { 
      cout << "Please insert an int for matr [" << i << "] [" << j << "]" << endl; 
      cin >> cell; 
      this->matr[i][j] = cell; //whatever "this" object is, store its matrix 
     } 
    } 
}//storeMatrix 

void Matrices::printMatrix() 
{ 
    for (int i = 0; i < this->matr.size(); i++) 
    { 
     for (int j = 0; j < this->matr[i].size(); j++) 
     { 
      cout << this->matr[i][j]; // whatever "this" objecct is, print its matrix 
     } 
     cout << endl; 
    } 
}//printMatrix 
+0

誰が、なぜダウン票ですか?おそらく、私が紛失しているものを学ぶ必要があるかもしれません。 –

+1

下の投票についてはわかりませんが、両方の関数の 'matr'引数を正しく削除したら、' this-> matr'を修飾する必要はありません。 – Useless

+0

@Useless Right。私は説明のためにそれを保つつもりです –

関連する問題