不能以任何方式读取函子类

Cannot read functor class in any way

本文关键字:读取 子类 何方 任何方 不能      更新时间:2023-10-16

好了,我正在实现一个动态二维矩阵类。对于基,这是我到目前为止得到的:

template <typename Type>
class dyMatrix {
    private:
        Type *mat;
        int lines, columns;
        int length;
        void assimilate (const dyMatrix<Type>& that) {
            lines = that.lines;
            columns = that.columns;
            length = that.length;
        }
    public:
        dyMatrix (int height, int width)
            : lines(height), columns(width), mat(0)
        {
            length = lines * columns;
            mat = new Type[length];
        };
        // ---
        int getLines() {
            return lines;
        };
        int getColumns() {
            return columns;
        };
        int getLength() {
            return length;
        }
        // ---
        Type& operator() (int i, int j) {
            return mat[j * columns + i];
        };
        Type& operator() (int i) {
            return mat[i];
        };
        // ---
        dyMatrix (const dyMatrix<Type>& that) {
            this->assimilate(that);
            // ---
            mat = new Type[length];
            for (int i = 0; i < length; i++) {
                mat[i] = that.mat[i];
            }
        };
        dyMatrix& operator= (const dyMatrix<Type>& that) {
            Type *local_mat = new Type[that.length];
            for (int i = 0; i < that.length; i++) {
                local_mat[i] = that.mat[i];
            }
            // ---
            this->assimilate(that);
            delete[] mat;
            mat = local_mat;
            // ----
            return *this;
        };
        ~dyMatrix() {
            delete mat;
        };
};

我的问题是,我一直有一些特定的问题,当试图读取输入到它。例如,我有下面的main():

int main() {
    int lanes, cols;
    cin >> lanes >> cols;
    dyMatrix<int> map(lanes, cols);
    /* CONTINUE READING */
    cout << endl;
    for (int i = 0; i < lanes; i++) {
        for (int j = 0; j < cols; j++) {
            cout << map(i, j) << ' ';
        }
        cout << endl;
    }
}

是注释部分,如果我放入以下代码行:

int aux;
for (int i = 0; i < lanes; i++) {
    for (int j = 0; j < cols; j++) {
        cin >> map(i, j);
    }
}

或:

int aux;
for (int i = 0; i < lanes; i++) {
    for (int j = 0; j < cols; j++) {
        cin >> aux;
        map(i, j) = aux;
    }
}

甚至:

int aux;
for (int i = 0; i < lanes; i++) {
    for (int j = 0; j < cols; j++) {
        cin >> aux;
        map(i, j) = i + j; // !!!
    }
}

…这行不通。然而,由于某些原因,以下内容将:

int aux;
for (int i = 0; i < lanes; i++) {
    for (int j = 0; j < cols; j++) {
        // cin >> aux;
        map(i, j) = i + j; // Still the same thing as before, logically
    }
}

我不明白。这里发生了什么?


编辑:对不起,忘了包括我的调试过程。

输入:

3 5
1 2 3 4 5
1 2 3 4 5

第一行是矩阵的维数。第二行和第三行是写入其中的值;然而,在第三行末尾按下Enter键后,程序崩溃了。

它甚至不能用这个输入:

3 5
1
2
3
4
5
1
2
3
4
5

在第二个5之后又崩溃了

你的索引出现了错误的方式:

    Type& operator() (int i, int j) {
        return mat[j * columns + i];
    };

这里,i显然是列,j是行。然而,在循环中,参数以相反的顺序出现:

for (int i = 0; i < lanes; i++) {
    for (int j = 0; j < cols; j++) {
        map(i, j) = ...; // row then column
    }
}

并且,析构函数应该使用delete[]而不是delete