私のコードは[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;
}
どのようにそれを修正するためのアイデア?
あなたはstoreMatrix' 'のための参照を必要とする程度ですね。 'printMatrix'では、必要ではないはずですが、const refはパフォーマンスを節約できます。さらに、あなたのデザインは少し不明です。 'printMatrix'は、格納された行列を出力するか、渡された行列を出力しますか?なぜ、関数のパラメータは、クラスのパラメータと同じ名前ですか? – AndyG
printMatrixはstoreMatrixに格納された行列を出力する必要があります – Celestiny