为什么我的函数不改变对象的属性

why my function doesn't change my object's attribute

本文关键字:对象 属性 改变 我的 函数 为什么      更新时间:2023-10-16

我正在尝试使用C 向量进行矩阵类,但是我不知道为什么我的函数内部的"矩阵结果"的内部没有传递给我的对象保留在功能中。

对于到目前为止的半拟合性,我只是尝试在两个矩阵中执行"加法功能"。

我尝试使用指针,但是(根据我的知识(,我不能在此明智的情况下将我的效果称为对象:

foo.function1(bar1).function2(bar2);

但是使用指针我必须以这种方式调用函数:

foo.function1(bar1);
foo.function2(bar2);
//and so on..

这是我的标题文件:

#include <iostream>
#include <vector>
using namespace std;

class Matrix 
{
    public:
        Matrix (int height, int width);
        Matrix add(Matrix m);
        Matrix applyFunction(double (*function)(double));
        void print();

    private:
        vector<vector<double> > matrix;
        int height;
        int width;
};

这是.cpp文件:

Matrix::Matrix(int height, int width)
{
    this->height = height;
    this->width = width;
    this->matrix = vector<vector<double> >(this->height, vector<double>(this->width));
}
Matrix Matrix::add(Matrix m)
{
    Matrix result(this->height, this->width);
    if (m.height== this->height&& m.width== this->width)
    {
        for (int i = 0; i < this->height; i++)
        {
            for (int j = 0; j < this->width; j++)
            {
                result.matrix[i][j] = this->matrix[i][j] + m.matrix[i][j];
            }
            return result;
        }
    }
    else
    {
        cout << "Impossible to do addition, matrices doesn't have the same dimension" << endl;
        return result;
    }
}
Matrix Matrix::applyFunction(double(*function)(double))
{
    Matrix result(this->height, this->width);
    for (int i = 0; i < this->height; i++)
    {
        for (int j = 0; j < this->width; j++)
        {
            result.matrix[i][j] = (*function)(this->matrix[i][j]);
        }
    }
    return result;
}
void Matrix::print()
{
    for (int i = 0; i < this->height; i++)
    {
        for (int j = 0; j < this->width; j++)
        {
            cout << this->matrix[i][j] << " ";
        }
        cout << endl;
    }
    cout << endl;
}

输出应为添加的啤酒a B 2x2:

x1 x2

x3 x4

但是计算机仅显示零。

您的成员函数均返回一个新对象(它们返回" value"(。

从您对链接的使用中,您似乎实际上想修改对象并通过参考返回*this

否则您需要以下内容:

auto bar2 = foo.function1(bar1);
auto bar3 = foo.function2(bar2);
// etc

目前没有指针。

有两个变体如何实现 add

Matrix add(Matrix m)
{
    // optimisation: you don't need separate result, m already IS a copy!
    // so you can just calculate:
    ...
    {
        m.matrix[i][j] += this->matrix[i][j];
    }
    return m;
}

或:

Matrix& add(Matrix const& m)
//                    ^ accept const reference to avoid unnecessary copy
//    ^ returning reference(!)
{
    ...
    {
        // modifies itself!
        this->matrix[i][j] += m.matrix[i][j];
    }
    return *this; // <- (!)
}

现在允许这样做:

Matrix m0, m1, m2;
m0.add(m1).add(m2);
// m0 now contains the result, original value is lost (!)

因此,您不需要第一个变体中的最终作业:

m0 = m0.add(m1).add(m2);
// or assign to a new variable, if you want to retain m0's original values

这是您问题所缺乏的(因此您没有得到所需的结果(。

也许您想拥有两个变体,并且您可能会重命名其中之一。但是C 中有一个不错的功能,您可能会更喜欢:操作员超载。考虑普通的int:

int n0, n1;
n0 += n1;
int n2 = n0 + n1;

好吧,假设您知道发生了什么。如果您可以对矩阵完全一样?实际上,你可以!您需要做的是使操作员超载:

Matrix& operator+=(Matrix const& m)
{
    // identical to second variant of add above!
}
Matrix operator+(Matrix m) // again: the copy!
{
    // now implement in terms of operator+=:
    return m += *this;
}

是的,现在您 can 做:

Matrix m0, m1, m2;
m0 += m1 += m2;
m2 = m1 + m0;

另外(我更喜欢(您也可以将第二操作员(operator+(作为免费的Standing功能:

// defined OUTSIDE Matrix class!
Matrix operator+(Matrix first, Matrix const& second)
{
    return first += second;
}

最后:如果尺寸不匹配,那么比返回一些虚拟矩阵会抛出一些例外。std::domain_error可能是候选人的候选者,或者您定义自己的例外,例如SizeMismatch。而且,请不要输出任何内容或在此类运营商中的其他地方输出任何东西,这不是任何人对他们的期望,此外,您将控制台输出向其他可能认为不合适的人征收(也许他们希望用另一种语言输出?(。<<<<<<<<<<<<<<