简体   繁体   English

C ++ 2d数组添加方法帮助构建要添加和其他数学对象的2d矩阵

[英]C++ 2d array add method help for building a 2d matrix to add and other mathematical objects

I was wondering how to go about this. 我想知道如何去做。 I have a matrix object that is a 2d array that grabs a matrix. 我有一个矩阵对象,它是一个捕获矩阵的二维数组。 I am trying to build an add method to add my matrices together.I am a second semester student and have more methods but i am stuck on how to go about this. 我正在尝试建立一个add方法来将我的矩阵加在一起。我是第二个学期的学生,并且有更多的方法,但是我仍然坚持如何做到这一点。 we have done up to overloading operators, dynamic memory and pointers. 我们已经完成了重载运算符,动态内存和指针的工作。 I just need some guidance to help me learn this stuff effectively instead of trying for 12 hours. 我只需要一些指导即可帮助我有效地学习这些知识,而不是尝试12个小时。 Any help would be grateful. 任何帮助将不胜感激。

my class function is 我的班级职能是

Matrix Matrix:: add(const Matrix & m) const
{
    Matrix v; // I know to make a matrix to add

    // I tried this but seems to not work
    v=v[rows][cols]+ m[rows][cols];
    return v
}

I think I have figured it out now. 我想我已经知道了。 Thanks! 谢谢! But i will still vote best answer so have at it! 但是,我仍然会投票给最佳答案,敬请期待! It might be useful for other students. 这可能对其他学生有用。

You should overload operator+ and operator+= to do this. 您应该重载operator+operator+=来做到这一点。

Matrix& operator+ (Matrix lhs, const Matrix& rhs)
{
    //do your addition
    return *this;

}

For more detailed information on operator overloading, see this question thread 有关运算符重载的更多详细信息,请参阅问题线程

I'm going to take a stab and assume your matrix class looks something like this: 我要刺一下,并假设您的矩阵类看起来像这样:

class Matrix
{
    public:
        int width;
        int height;
        int data[width][height];
}

If you want to add two Matrixes then you need some sort of function that takes two Matrixes and produces a third matrix such that each value in data is the addition of the two corresponding values in the original matrixes. 如果要添加两个矩阵,则需要某种函数,该函数需要两个矩阵并生成第三个矩阵,以便数据中的每个值都是原始矩阵中两个对应值的加法。 One such function might look like this: 一个这样的函数可能看起来像这样:

Matrix add(const Matrix& lhs, const Matrix& rhs)
{
    Matrix output;
    for(int row = 0; row < lhs.height; ++row)
    {
        for(int column = 0; column < lhs.widtht; ++column)
        {
            output.data[row][column] = lhs.data[row][column] + rhs.data[row][column];
        }
    }
    return output;
}

We're going to assume that the matrixes are the same size and ignore the problems different sized matrixes cause. 我们将假设矩阵的大小相同,而忽略大小不同的矩阵导致的问题。 So all we do is loop through both matrixes and add the values to a new matrix. 因此,我们要做的就是遍历两个矩阵并将值添加到新矩阵中。 Now we can call code like: 现在我们可以像这样调用代码:

Matrix a; // Pretend this already has data
Matrix b; // Ditto
Matrix c = add(a, b);

You can see where this is going, all that's left is to make it an operator, generally when you type 您可以看到它的去向,剩下的就是让它成为一个运算符,通常在您键入时

a + b

What the compiler actually see's is 编译器实际看到的是

operator+(a, b)

So all that's left is to rename the add function to 所以剩下的就是将add函数重命名为

Matrix operator+(const Matrix& lhs, const Matrix& rhs)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM