抛出异常:读取访问冲突。 this-> pt_mat是0x1110112

时间:2018-03-04 04:12:10

标签: visual-c++

我在类函数中使用双指针设置系数并在main中使用它时出现此错误。 类函数在独立函数中用于使用类类型指针设置系数。

The class code is:
class Matrix
{
private:
    int size_m, size_n;
    double **pt_mat;
public:
    Matrix() {};
    Matrix(int m, int n)
    {
        pt_mat = new double *[m];
        for (int i = 0; i < m; i++)
        {
            pt_mat[i] = new double[n];
        }
    }
    void set_size(int m, int n)
    {
        size_m = m;
        size_n = n;
    }
    void set_coef(int i, int j, double x)
    {
        pt_mat[i][j] = x;
    }
    void get_size(int *pt_m, int *pt_n)
    {
        *pt_m = size_m;
        *pt_n = size_n;
    }
    double get_coef(int i, int j)
    {
        return pt_mat[i][j];
    }
    void print(ofstream &fout)
    {
        fout << size_m << " " << size_n << endl;
        int i, j;
        for (i = 0; i < size_m; i++)
        {
            for (j = 0; j < size_n; j++)
            {
                fout << pt_mat[i][j] << " ";
            }
            fout << endl;
        }
    }

    void max_min(int *pt_imax, int *pt_jmax, double *pt_max, int *pt_imin, int *pt_jmin, double *pt_min)
    {
        double max, min;
        int i, j, imax = 0, imin = 0, jmax = 0, jmin = 0;
        max = pt_mat[0][0];

        for (i = 0; i < size_m; i++)
        {
            for (j = 0; j < size_n; j++)
            {
                if (pt_mat[i][j] > max)
                {
                    max = pt_mat[i][j];
                    imax = i;
                    jmax = j;
                }
            }
        }

        *pt_max = max;
        *pt_imax = imax;
        *pt_jmax = jmax;

        min = pt_mat[0][0];

        for (i = 0; i < size_m; i++)
        {
            for (j = 0; j < size_n; j++)
            {
                if (pt_mat[i][j] < min)
                {
                    min = pt_mat[i][j];
                    imin = i;
                    jmin = j;
                }
            }
        }

        *pt_min = min;
        *pt_imin = imin;
        *pt_jmin = jmin;
    }
};

独立功能:

void mat_add(Matrix a, Matrix b, Matrix *pt_c)
{
    Matrix c; 
    pt_c = &c;
    int a_m, a_n, *p_am = &a_m, *p_an = &a_n;
    a.get_size(p_am, p_an);
    c.set_size(a_m, a_n);

    int m, n, *pt_m = &m, *pt_n = &n;
    double coef;
    a.get_size(pt_m, pt_n);
    int i, j;
    for (i = 0; i < m; i++)
        for (j = 0; j < n; j++)
        {
            coef = a.get_coef(i, j) + b.get_coef(i, j);
            c.set_coef(i, j, coef);
        }
}
如果可以的话,请帮助我。我没有语法错误shows.enter code here

0 个答案:

没有答案