【问题标题】:How can I take object from function?如何从函数中获取对象?
【发布时间】:2020-01-09 05:40:22
【问题描述】:

我写了一个简单的程序来计算一些矩阵,并陷入了这个问题。我无法取出我的新矩阵。

这是我的 Matrix.h

#pragma once
using namespace std;
class Matrix
{
private:
    int row, col;
    float **matrix;
public:
    Matrix(int); // square matrix
    Matrix(int, int); // matrix with different rows and columns
    ~Matrix(); //delete
    void set(int, int, double); // set value to matrix
    double get(int, int); // get value from matrix
    void print(); // display matrix
    int rows(); // show rows
    int cols(); //show columns
    Matrix operator*(Matrix); // multiply matrix
};
#include <iostream>
#include <fstream>
#include "Matrix.h"
using namespace std;

Matrix::Matrix(int row) {
    if (row <= 0) {
        cout << "To small value for ROW or COL";
        exit(0);
    }
    this->row = row;
    this->col = row;
    this->matrix = new float* [row];

    for (int i = 0; i < row; i++)
    {
        this->matrix[i] = new float[row];

        for (int j = 0; j < row; j++)
        {
            this->matrix[i][j] = 0;
        }
    }
}

Matrix::Matrix(int row, int col) {
    if (row <= 0 || col <= 0) {
        cout << "To small value for ROW or COL";
        exit(0);
    }
    this->row = row;
    this->col = col;
    this->matrix = new float* [row];

    for (int i = 0; i < row; i++)
    {
        this->matrix[i] = new float[col];

        for (int j = 0; j < col; j++)
        {
            this->matrix[i][j] = 0;
        }
    }
}

Matrix::~Matrix() {
    for (int i = 0; i < this->row; i++)
    {
        delete[] matrix[i];
    }
    delete[] matrix;
}

int Matrix::rows() {
    return this->row;
}

int Matrix::cols() {
    return this->col;
}
void Matrix::set(int row, int col, double val) {
    if (row > this->row || col > this->col || row < 0 || col < 0) {
        cout << "There is no value to set.";
        exit(0);
    }
    else {
        this->matrix[row - 1][col - 1] = val;
    }
}
double Matrix::get(int row, int col) {
    if (row > this->row || col > this->col || row < 0 || col < 0) {
        cout << "There is no value, please correct ROW or COL.";
        exit(0);
    }
    else {
        cout << "Taken value from row " << row << " and col " << col << " = ";
        return this->matrix[row - 1][col - 1];
    }
}

void Matrix::print() {
    for (int i = 0; i < this->row; i++)
    {
        for (int j = 0; j < this->col; j++)
        {
            cout << this->matrix[i][j] << " ";
        }
        cout << endl;
    }
    cout << endl;
}

Matrix Matrix::operator*(Matrix B) {
        Matrix multiplied(B.row, this->col);

        for (int i = 0; i < this->row; i++) {
            for (int j = 0; j < B.col; j++) {
                multiplied.matrix[i][j] = 0;
                for (int k = 0; k < this->col; k++) {
                    multiplied.matrix[i][j] += this->matrix[i][k] * B.matrix[k][j];
                }
        }
        }
        multiplied.print(); // this work, and show correct answer in console
        return multiplied;
}
#include <iostream>
#include <fstream>
#include "Matrix.h"
using namespace std;
int main()
{  
    Matrix one(8,7), two(8,7), three(1,1), four(5);
    one.set(1, 1, 5);
    cout << one.get(1,5) << endl;
    one.print();
    two.set(1,2,2);
    two.print();
    Matrix nine = two * one; // but two*one from Matrix.cpp gives me correct Matrix
    nine.print(); // this stop the program
}

我得到这样的东西: makefile:4: 目标“运行”的配方失败 make: *** [run] 分段错误(核心转储)

我正在使用 makefile 来运行此代码。问题出在哪里?

【问题讨论】:

  • 这能回答你的问题吗? What is The Rule of Three? 特别是return multiplied; 中的Matrix::operator*尝试返回一个副本并删除原始(在本地堆栈上),导致成员matrix 的双重删除。

标签: c++


【解决方案1】:

对您的问题的评论指出了事情失败的原因 - 您没有定义复制构造函数,因此编译器为您隐式定义了一个。隐式复制构造函数将您的matrix 指针复制到一个新的Matrix 中,然后在operator*() 调用内部和返回新矩阵的外部都将其删除。您可以通过定义复制和移动构造函数来修复它,但我认为有更好的方法。

首先,如果您发现自己在代码中手动使用了newdelete,这在现代C++ 中通常是错误的。其次,您不需要为每一行分配新数组 - 它们的大小都相同,您可以进行一次分配。

#pragma once

#include <vector>

// never use namespace aliases in header files - this polutes the global namespace of every user.

class Matrix
{
private:
    size_t rows; // unsigned integers prevent values less than 0
    size_t cols;
    std::vector<double> elements;
public:
    Matrix(size_t);
    Matrix(size_t, size_t);
    // destructor no longer needed! vector handles it
    void set(size_t, size_t, double);
    double get(size_t, size_t) const; // follow const correctness
    void print();
    size_t rows();
    size_t cols(); 
    Matrix operator*(Matrix);
};

一些实现:

// This is the whole constructor. Vector will zero-initialize all the values
Matrix::Matrix(size_t row, size_t col)
    : rows(row)
    , cols(col)
    , elements(row * col)
{ }

void Matrix::set(size_t row, size_t col, double val) {
    elements[row * cols + col] = val;
}
double Matrix::get(size_t row, size_t col) const {
    return elements[row * cols + col];
}

【讨论】:

    猜你喜欢
    • 2019-09-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-10-12
    • 1970-01-01
    相关资源
    最近更新 更多