【问题标题】:Overloading operator* for a matrix class矩阵类的重载运算符*
【发布时间】:2012-05-04 15:33:51
【问题描述】:

我有一个矩阵:

#ifndef MATRIX_H
#define MATRIX_H

class Matrix
{   
    public:
        Matrix(int rows, int columns);
        Matrix(int, int, int** matrix); 
        Matrix(Matrix* copy);
        ~Matrix();

        void Set(int, int, int);
        void SetMatrix(int, int, int** matrix);             
        void Print();
        void ZeroMatrix(int,int,int** matrix);      
        void Add(Matrix* B);
        void Subtract(Matrix* B);
        void Copy(Matrix* B);

        int** Multiply(Matrix* B);
        int** Create(int,int);
        int** Get();
        int** Transpose();
        int** Scalar(int);

        int Get(int,int);
        int Rows();
        int Columns();

        Matrix operator*(int);

    private:
        int** _matrix;
        int _rows;
        int _columns;
};

#endif

下面是实现:

Matrix Matrix::operator*(int scale)
{
    return Matrix(_rows, _columns, Scalar(scale));
}

对于学校作业,我们必须重载多重运算符以使用整数标量。问题是我不断收到此错误:

main.cpp:在函数“int main(int, char*)”中: main.cpp:18:15:错误:'4 * B'中的'operator'不匹配

破解密码:

#include "Matrix.h"
#include <fstream>
#include <iostream>

int main(int argc, char *argv[])
{   
    Matrix* A = new Matrix(4,2);

    A->Set(0,0,1);  
    A->Set(0,1,2);
    A->Set(1,0,3);
    A->Set(1,1,4);  
    A->Print();

    Matrix B(A);
    B.Print();

    Matrix C(4 * B); //this line
    C.Print();


    delete A;

    return 0;
}

有什么想法吗?

编辑#1:

代码:

Matrix operator*(int); 
        Matrix operator* (int, const Matrix &);

生成:

In file included from main.cpp:1:0:
Matrix.h:31:40: error: ‘Matrix Matrix::operator*(int, const Matrix&)’ must take either zero or one argument
In file included from matrix.cpp:1:0:
Matrix.h:31:40: error: ‘Matrix Matrix::operator*(int, const Matrix&)’ must take either zero or one argument
matrix.cpp:207:50: error: ‘Matrix Matrix::operator*(int, const Matrix&)’ must take either zero or one argument

【问题讨论】:

  • 顺便说一句,你不需要在主文件中使用fstreamiostream,因为它们中的任何一个都没有在 main 中实际使用。最好只包含每个文件所需的内容。
  • 作业应该有作业标签
  • @chris:这是我之前测试的东西。

标签: c++ matrix operator-overloading


【解决方案1】:

当你指定你的成员函数时,你的类必须在左边。

B * 4 等价于B.operator* (4)。当你说4 * B 时,这不起作用。

要解决这个问题,只需使用B * 4 而不是4 * B,或提供外部重载

Matrix operator* (int, const Matrix &);

然后,4 * B 将匹配此重载。

【讨论】:

  • 你在做Matrix Matrix::operator*,而你应该(如上面发布的)做Matrix operator*(因此,在课堂之外定义它(或者在外部,用克里斯的话来说))
  • @Jasper,谢谢,我想知道出了什么问题。我做了自己的小测试,效果很好。 @StormKiernan,记住 B * 4 与您现在拥有的兼容:/
  • @StormKiernan,我不确定您是否这样做,但您需要为 Matrix operator* (int, const Matrix &amp;) 定义一个正文。使用与其他运算符重载相同的主体。如果您需要访问私有成员,请将其声明为您班级中的朋友。它上面的那个,只有一个 int 是没有意义的。
  • 除了这些建议之外,您还可以在类之外定义一个重载“*”运算符的函数,并将其作为两个 Matrix 对象的参数。使这个函数成为类的朋友,然后使用转换构造函数来处理作为第一个或第二个参数中的参数的传递。如果你想要一个例子,请告诉我。
【解决方案2】:

这将在任一方向工作......

#include <iostream>

class Matrix
{
public:
  Matrix(int x) // This works as a convert constructor
    : _x(x) { } // if you don't use the explicit keyword

  friend Matrix operator*(const Matrix& left, const Matrix& right);

  int _x;
};

Matrix operator*(const Matrix& left, const Matrix& right)
{
  return Matrix(left._x * right._x);
}

int main()
{
  Matrix m(3);
  int a = 4;

  Matrix m1(m * a);
  Matrix m2(a * m);

  std::cout << m._x  << endl  // 3
            << a     << endl  // 4
            << m1._x << endl  // 12
            << m2._x << endl; // 12
}

【讨论】:

    猜你喜欢
    • 2015-07-29
    • 1970-01-01
    • 1970-01-01
    • 2016-02-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多