来自一个类的c ++第二个函数不能访问第一个函数

时间:2016-04-30 16:12:55

标签: c++ oop matrix

我的代码将整数存储在[n] x [n]矩阵中。但是当我使用printMatrix()方法时,它只在命令提示符下返回零,而不是我之前分配给矩阵的整数。我想我必须使用引用/指针,但我不太清楚如何。这是我的代码:

#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;
}

任何想法如何解决?

1 个答案:

答案 0 :(得分:1)

删除与printMatrix()相关的所有参数。在main()和它的原型(在类中)和它的定义void YourClassName::printMatrix()。然后在其定义中设置:

cout << this->matr[i][j]; //有关详细说明,请参阅下文

而不是:

cout << matr[i][j];

添加This将打印&#34; 此当前实例的私有成员&#34;您已经填充了上一个函数中的数据。

同样在您的store()中,您需要&#34;链接&#34;您正在使用当前实例做什么。

总体而言:此设计应该可以帮助您更好地了解正在发生的事情。看到这个主要():

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