Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/fortran/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ c++;矩阵类:重载赋值运算符_C++_Class_Matrix_Operator Overloading_Assignment Operator - Fatal编程技术网

C++ c++;矩阵类:重载赋值运算符

C++ c++;矩阵类:重载赋值运算符,c++,class,matrix,operator-overloading,assignment-operator,C++,Class,Matrix,Operator Overloading,Assignment Operator,我在为矩阵类实现赋值运算符时遇到一些问题。编译器似乎不想识别我的重载赋值运算符(我想是吧?),我不知道为什么。我知道在C++中实现矩阵类的问题有很多关于因特网的文章(这帮助我走了这么远),但是这次我似乎无法把我目前的困境与其他任何帮助相平行。无论如何,如果有人能帮我解释一下我做错了什么,我将不胜感激。谢谢 以下是我的错误消息: In file included from Matrix.cpp:10: ./Matrix.h:20:25: error: no function named 'oper

我在为矩阵类实现赋值运算符时遇到一些问题。编译器似乎不想识别我的重载赋值运算符(我想是吧?),我不知道为什么。我知道在C++中实现矩阵类的问题有很多关于因特网的文章(这帮助我走了这么远),但是这次我似乎无法把我目前的困境与其他任何帮助相平行。无论如何,如果有人能帮我解释一下我做错了什么,我将不胜感激。谢谢

以下是我的错误消息:

In file included from Matrix.cpp:10:
./Matrix.h:20:25: error: no function named 'operator=' with type 'Matrix &(const Matrix &)'
      was found in the specified scope
        friend Matrix& Matrix::operator=(const Matrix& m);
                               ^
Matrix.cpp:79:17: error: definition of implicitly declared copy assignment operator
Matrix& Matrix::operator=(const Matrix& m){ //m1 = m2
                ^
Matrix.cpp:89:13: error: expression is not assignable
                        &p[x][y] = m.Element(x,y);
                        ~~~~~~~~ ^
3 errors generated.
以下是我的.cpp文件中的分配运算符代码:

  Matrix& Matrix::operator=(const Matrix& m){ //m1 = m2
      if (&m == this){
          return *this;
      }   
      else if((Matrix::GetSizeX() != m.GetSizeX()) || (Matrix::GetSizeY()) != m.GetSizeY()){
          throw "Assignment Error: Matrices must have the same dimensions.";
      }   
      for (int x = 0; x < m.GetSizeX(); x++)
      {
          for (int y = 0; y < m.GetSizeY(); y++){
              &p[x][y] = m.Element(x,y);
          }   
      }   
      return *this;
矩阵和矩阵::运算符=(常数矩阵和m){//m1=m2
如果(&m==此){
归还*这个;
}   
如果((矩阵::GetSizeX()!=m.GetSizeX())| |(矩阵::GetSizeY())!=m.GetSizeY()){
抛出“赋值错误:矩阵必须具有相同的维度。”;
}   
对于(int x=0;x
这是我的矩阵头文件:

   class Matrix
   {
    public:
      Matrix(int sizeX, int sizeY);
      Matrix(const Matrix &m);
      ~Matrix();
      int GetSizeX() const { return dx; }
      int GetSizeY() const { return dy; }
      long &Element(int x, int y) const ;       // return reference to an element
      void Print() const;

      friend std::ostream &operator<<(std::ostream &out, Matrix m);
      friend Matrix& Matrix::operator=(const Matrix& m);
      long operator()(int i, int j);
      friend Matrix operator*(const int factor, Matrix m); //factor*matrix
      friend Matrix operator*(Matrix m, const int factor); //matrix*factor
      friend Matrix operator*(Matrix m1, Matrix m2); //matrix*matrix
      friend Matrix operator+(Matrix m1, Matrix m2);
类矩阵
{
公众:
矩阵(int-sizeX,int-sizeY);
矩阵(常数矩阵&m);
~Matrix();
int GetSizeX()常量{return dx;}
int GetSizeY()常量{return dy;}
long&Element(intx,inty)const;//返回对元素的引用
无效打印()常量;

friend std::ostream&operator您的赋值运算符应该是成员函数,而不是
friend
。您的其他运算符应该将参数作为
常量矩阵&
,否则您将复制运算符使用的矩阵对象。

您正在获取元素的地址:

 &p[x][y] = m.Element(x,y);
当您要分配给它时,如下所示:

p[x][y] = m.Element(x,y);

友元矩阵和矩阵::运算符=(常数矩阵和m);

上述部分没有什么意义,因为必须将
操作符=
定义为类中的成员函数

事实上,我认为如果你不需要在这里交朋友(或者至少不需要这么多朋友),你的生活会轻松得多。矩阵的自给自足的公共接口应该允许你实现所有想要的操作符,不管他们是否是这个类的成员


也许您首先应该寻求的是一个自给自足的公共接口,这样您就不需要像矩阵乘法运算符这样的东西作为朋友,因为否则,可能的诱惑是让涉及矩阵的每个操作都需要访问矩阵内部。

删除d前面的朋友头文件中的eclaration。
friend Matrix&Matrix::operator=(const Matrix&m);
什么?为什么?另外,请发布一个没有行号的文件,以便方便复制。“friend…你一直在使用这个词。我不认为它的意思是你认为它的意思。”类中唯一应该声明为friend运算符的方法是与ostream对象一起工作的stream运算符。与类本身相关的所有其他运算符都不能是friend运算符。但是,如果您想要更快的实现,则可以将运算符声明为inline!啊,我明白了。一位朋友建议添加ng'friend'在他帮助我时,我并没有对此提出疑问,但我继续删除了它们,因为它们是多余的。很好的捕获!我甚至没有在查看即时生成错误时发现这一点。谢谢!这个和其他注释帮助修复了我的代码。谢谢!这个和其他注释帮助修复了我的代码。