分配给C ++类中的数组的正确方法是什么?

Mat*_*ard 0 c++ oop matrix

我有这个C ++类Matrix(请参见下面的代码片段)。

在我的randomize方法中,我设置了matr的所有值(matr是2x2矩阵)。

当我调用print_matrix时,它正在复制元素(1,0)和(1,1)并将它们都打印两次,以及不打印(0,0)或(0,1)。

我做错了什么?

请参见下面的输出。

在此处输入图片说明

class Matrix {

public:


    int rows;
    int cols;
    double rnd;

    double* matr;

    Matrix(int a, int b) {

        printf(" vals %d %d \n", a, b);

        rows = a;
        cols = b;

        matr = new double[a, b];
        //this->print_matrix();
        //clear_matrix();
        //this->print_matrix();
        //this->randomize();
        //this->print_matrix();

    }



    double &at(int pos1, int pos2) {


        return matr[pos1, pos2];
    };

    void setPos(int pos1, int pos2, double value) {
        matr[pos1, pos2] = value;
    };

    void randomize() {

        for (int r = 0; r < rows; r++) {
            for (int c = 0; c < cols; c++) {
                rnd = double(rand()) / double((RAND_MAX)+1.0);
                printf("Rand : %d, C: %d, Val: %f \n",r,c, rnd);
                this->setPos(r, c, rnd);
                //matr[r, c] = rnd;
                printf("New value R: %d C: %d Val: %f \n", r, c, matr[r,c]);
                //rnd = 0;
            }
        }
    };

    void subtract_Scalar(double val) {
        double curr_Val = 0;
        double result = 0;
        for (int r = 0; r < rows; r++) {
            for (int c = 0; c < cols; c++) {
                curr_Val = this->at(r, c);
                result = curr_Val - val;
                this->setPos(r, c, 0);
                this->setPos(r, c, (float)result);
                //this->setPos(r, c, 5);
                //printf("SS CV : %f, Re: %f  \n", curr_Val, result);
                curr_Val = 0;
                result = 0;
            }
        }
    };

    void print_matrix() {
        for (int r = 0; r < rows; r++) {
            for (int c = 0; c < cols; c++) {
                printf("PM R : %d, C: %d Val: %f \n", r, c, matr[r,c]);
                //printf("%f", this->at(r, c));
            }
            //printf("\n");
        }
    }



    void clear_matrix() {
        for (int r = 0; r < rows; r++) {
            for (int c = 0; c < cols; c++) {

                this->setPos(r, c, 0.0);
            }
        }
    }

    };
Run Code Online (Sandbox Code Playgroud)

R S*_*ahu 8

由于以下行,您的程序具有未定义的行为:

printf("PM R : %d, C: %d Val: %f \n", r, c, matr[r,c]);
Run Code Online (Sandbox Code Playgroud)

matr[r,c]不访问矩阵的元素。由于使用了逗号运算符,因此简单地是matr[c],它的计算结果是一个指针。您正在使用打印指针%f。那是未定义的行为部分。您需要使用matr[r][c]来访问矩阵的元素。

printf("PM R : %d, C: %d Val: %f \n", r, c, matr[r][c]);
Run Code Online (Sandbox Code Playgroud)

使用matr[r, c]不正确。

有关更多详细信息,请参见逗号运算符的文档。


更新资料

由于@PeteBecker敏锐的眼睛,这个问题与我最初的想法不同。

事实证明,它matr是type double*。因此matr[c]计算结果为两倍。因此,该程序没有未定义的行为。不管的值是多少,它始终一直访问c-元素。matrr

问题开始于:

matr = new double[a, b];
Run Code Online (Sandbox Code Playgroud)

它必须是

matr = new double[a * b];
Run Code Online (Sandbox Code Playgroud)

使用矩阵元素进行访问的任何地方matr[r, c]都需要matr[r*cols + c]。

在中at,您需要使用:

return matr[pos1 * cols + pos2];
Run Code Online (Sandbox Code Playgroud)

在中setPos,您需要使用:

matr[pos1 * cols + pos2] = value;
Run Code Online (Sandbox Code Playgroud)

在中randomize,您需要使用:

printf("New value R: %d C: %d Val: %f \n", r, c, matr[r*cols + c]);
Run Code Online (Sandbox Code Playgroud)

在中print_matrix,您需要使用:

printf("PM R : %d, C: %d Val: %f \n", r, c, matr[r*cols + c]);
Run Code Online (Sandbox Code Playgroud)

可以简化代码用于通过提供的过载接入元件at用于const非constobjcts。

double& at(int r, int c) { return matr[r*cols + c]; }
double at(int r, int c) const { return matr[r*cols + c]; }
Run Code Online (Sandbox Code Playgroud)

然后,setPos可以实现为:

  void setPos(int pos1, int pos2, double value) {
     at(pos1, pos2) = value;
  };
Run Code Online (Sandbox Code Playgroud)

这些printf行可以更新为:

printf("New value R: %d C: %d Val: %f \n", r, c, at(r, c));
printf("PM R : %d, C: %d Val: %f \n", r, c, at(r, c));
Run Code Online (Sandbox Code Playgroud)