Home > Net >  It tells me I cannot convert line 19 and 20 bracelets into int
It tells me I cannot convert line 19 and 20 bracelets into int

Time:07-22

cannot convert 'brace-enclosed initializer list>' to 'int' in assignment as explained above, line 19 and 20, specifically when I described the content of the array, the compiler told me the line above. I know it is a long code, and I will appreciate your answers. I guess that's as detailed as I could.

#include <iostream>
using namespace std;

class matrix{
private:
  static const int x =3;
  int m1[x][x];
  int m2[x][x];
  int c[x][x];
public:
  matrix(){
     int m1[x][x];
     int m2[x][x];
     int c[x][x];
     c[x][x];

  }
matrix(int m1[x][x], int m2[x][x], int c[x][x]){
  m1[x][x]= {{1,2,3},{4,5,6},{7,8,9}};
  m2[x][x]= {{10,11,12},{13,14,15},{16,17,18}};
  c[x][x];
}

  matrix operator (matrix& m2){

    matrix result;
    for( int i=0; i<x; i  ){
      for(int j=0; j<x; j  ){
        result.m1[i][j];
        result.m2[i][j];
        result.c[i][j] = result.m1[i][j]   result.m2[i][j];
      }
    }

return result;
  }
  matrix operator-(matrix& m2){
    matrix result;
    for( int i=0; i<x; i  ){
      for(int j=0; j<x; j  ){
        result.m1[i][j];
        result.m2[i][j];
        result.c[i][j] = result.m1[i][j] - result.m2[i][j];
      }
    }
    return result;
  }
  matrix operator*(matrix& m2){
    matrix result;
    for( int i=0; i<x; i  ){
      for(int j=0; j<x; j  ){
        result.m1[i][j];
        result.m2[i][j];
        result.c[i][j] = result.m1[i][j] * result.m2[i][j];
      }
    }
    return result;
  }


  friend ostream& operator <<(ostream& outs, const matrix& m){
  outs << m<< endl;
  return outs;
}
};
int main(){
matrix m1;
matrix m2;
cout <<m1 m2<<endl;
return 0;
}

CodePudding user response:

As already said this code is just wrong from top to bottom. It really should be thrown away. Let fix the most basic error. class matrix represents a 3x3 matrix. So it should be written like this

class matrix
{
private:
    static const int x = 3;
    int elem[x][x]; // the elements of a 3x3 matrix
public:
    ... // more code
};

In your version

class matrix
{
private:
    static const int x =3;
    int m1[x][x];
    int m2[x][x];
    int c[x][x];
public:
    ... // more code
};

the class has three different 3x3 matrices. That isn't right.

So throw your code away, start again with class like the one I've written above.

Here's how operator looks with my version of the class

matrix operator (const matrix& m2) {
    matrix result;
    for (int i=0; i<x; i  ) {
        for (int j=0; j<x; j  ) {
            result.elem[i][j] = elem[i][j]   m2.elem[i][j];
        }
    }
    return result;
}

CodePudding user response:

It's really hard to understand what you are trying to do, You have parameters in the constructor and then you are reinitializing them again.

But I can help you with the way to initialize 2d Matix using the initializer list

matrix():m1{{1,2}, {3, 4}} ,...{}
  •  Tags:  
  • c
  • Related