如何从函数中获取对象?

How can I take object from function?

本文关键字:获取 取对象 函数      更新时间:2023-10-16

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

这是我的矩阵。

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

我得到这样的东西: 生成文件:4:目标"运行"的配方失败 make: *** [run] 分段错误(核心转储(

我正在使用生成文件来运行此代码。问题出在哪里?

对您的问题的评论指出了失败的原因 - 您没有定义复制构造函数,因此编译器为您隐式定义了一个。隐式复制构造函数将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];
}