如何从函数中获取对象?
How can I take object from function?
我写了一个简单的程序来计算一些矩阵,并坚持这个问题。我无法拿出我的新矩阵。
这是我的矩阵。
#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*()
。您可以通过定义复制和移动构造函数来修复它,但我认为还有更好的东西。
首先,如果您发现自己在代码中手动使用new
和delete
,那么在现代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];
}
相关文章:
- 如何在C++中获取该对象的类声明中对象的地址?
- 如何在新的派生对象中获取基本对象的数据?
- 数组中不同类型的对象并获取每个对象的类型
- Visual Studio 2017 C++,不能使用 typeid() 获取信息对象,缺少指针?;
- 从两个 4x64 位整数数组中获取取模
- 如何从参数包获取指向对象的指针
- 在DelphiInterface上实现Dynamic_cast以获取基础对象类的任何方法
- 在<Object>基于范围的 for 循环中从 std::vector 获取指向对象的指针
- boost::可选 - 对对象使用 boost::in_place,构造器通过引用获取其他对象
- 如何使用 API 获取 QML 对象的 id 属性C++
- 如何从指针到方法获取类(对象类型)
- 在 C/C++ 中获取 Java 对象的内存地址
- 在ClrProfiler中,如何从ObjectID中获取托管对象
- 从操作员内部获取有关对象的RTTI信息
- 使用C 获取Java对象来调用Java方法
- 我们可以从设备句柄中获取设备对象详细信息 /名称吗?
- 从qgraphicsitem qt C 获取实际对象
- 如何获取成员对象以通知其容器对象
- 如何从静态对象中获取该对象的值
- 如何获取 COM 对象的成员函数的地址?