0

Can someone please figure out the problems in this code for me. I am using code blocks 17.12. I am trying to make a Matrix class in which i want to initialize a matrix using a constructor and after then get the members of the array using a function. And then overload the '*' operator to multiply two entered matrices. And then overload the ostream to show the already given matrix as input or the product(like "cout<< m<< endl;).

#include <iostream>
using namespace std;

class Matrix
{
private:
    //static int row;                  //don't work
    //static const int row;            //don't work 
    //constexpr int row;               //don't work
    int row;
    int column;

//Here my moto is to make a matrix which takes input from the user and 
create the matrix of desired size at runtime.
    double A[row][column];

public:
    Matrix(int row,int column);
    Matrix(Matrix &mat);
    void setRowXColumn(int row,int column);
    void setColumn(int column);
    void setMatrix(Matrix A);
};


int main()
{
    //Here 3 and 2 are the rows and columns of the matrix m respectively.
    Matrix m(3,2);
    return 0;
}

Matrix::Matrix(int row=0,int column=0)          
{
    setRowXColumn(int row,int column);       //error: expected primary-expression before 'int'|
                                             //what primary-expression?
}

Matrix::Matrix(Matrix &mat)
{
    row=mat.row;
    column=mat.column;
}


void Matrix::setRowXColumn(int row,int column)
{
    if(row<0)
        this->row=0;
    else
        this->row=row;
    if(column<0)
        this->column=0;
    else
        this->column=column;
 }
//And i also want the members as input by the user at runtime.
void Matrix::setMatrix(Matrix A)
{
    for(int i=0;i<row;i++)
     {
        for(int j=0;j<column;j++)
        {
            cout<<"Enter"<<Matrix A<<"["<<i<<"]"<<"["<<j<<"]"<<endl;
            cin>>A[i][j];
        }
    }
}

From the above code i am getting the following errors.

||=== Build: Debug in Class Matrix (compiler: GNU GCC Compiler) ===|

Class Matrix\main.cpp|9|error: invalid use of non-static data member 'Matrix::row'|

Class Matrix\main.cpp|7|note: declared here|

Class Matrix\main.cpp|9|error: invalid use of non-static data member 'Matrix::column'|

Class Matrix\main.cpp|8|note: declared here|

Class Matrix\main.cpp||In constructor 'Matrix::Matrix(int, int)':|

Class Matrix\main.cpp|42|error: expected primary-expression before 'int'|

Class Matrix\main.cpp|42|error: expected primary-expression before 'int'|

Class Matrix\main.cpp||In member function 'void Matrix::setMatrix(Matrix)':|

Class Matrix\main.cpp|69|error: expected primary-expression before 'A'|

Class Matrix\main.cpp|70|error: no match for 'operator[]' (operand types are 'Matrix' and 'int')|

||=== Build failed: 6 error(s), 0 warning(s) (0 minute(s), 0 second(s)) ===|

I totally appreciate your help and thank you. I am a student an currenty learning c++. I am still working on this code.

Edit:-So far i have reduced the errors but "double A[row][column] is the main headache to me. I want it like this because i want to create a matrix like what i did in the main function. And then take the members of array as input next. Hope this edit clarifies my question further.

Thank you...

Cruz
  • 45
  • 6
  • 2
    `double A[row][column];` is not legal. The size of an array must be known at compile time. Some compilers allow you to get away with certain types of Variable Length Arrays, but not this usage. – user4581301 Mar 02 '19 at 07:54
  • 1
    Here is [a link to a very simple, very robust matrix class](https://stackoverflow.com/a/2076668/4581301) you can use as a starting point or inspiration. Note how it uses a 1 dimensional array and performs the indexing math to make it look like a 2D array. – user4581301 Mar 02 '19 at 07:58
  • 2
    Suggestion: Write less code before compiling and testing. If you only write a few lines, a function at the most, you find errors faster and they don't get as much of a chance to build up. Bugs have a tendency to gang up on you if you let them. Don't let them. – user4581301 Mar 02 '19 at 08:13
  • 1
    There are so many errors and misunderstandings in this code. And so many different kinds of errors. I know you are a beginner but you are in way over your head at the moment. Forget all the other advice you are getting, what user4581301 said is the most important thing. Start this project again (I don't think the code you've written so far is worth saving). Write a few lines of code at a time, get those lines compiled and tested and working **before** you write any more code. That way you only have **one problem to deal with at a time**. – john Mar 02 '19 at 08:28
  • Thanks for the answers. I am currently working on what you said to me and trying to learn from my mistakes. I will soon come up with edits in my code to make it working. Thanks – Cruz Mar 02 '19 at 17:48

1 Answers1

2
  1. void Marix::setRowXColumn(int row,int column) . it should Matrix. Are you using a IDE, it should warn you of those typos.

  2. setRowXColumn(int row,int column) should besetRowXColumn(row,column);

  3. c++ statements alway requires a ";" at the end.

  4. double A[row][column]; if you were trying to create a 'dynamic array', do it this way double **A; . and

        A = new double*[row];
    
        for(int i = 0; i < row; i++){
            A[i] = new double[column];
        }
    

    in your constructor, then delete it in you deconstructor.

    I think you can just use vector instead of array in this case.

rlongying
  • 73
  • 1
  • 8
  • 1
    4 is correct, but often has terrible cache usage and poor performance because it is an array made up of other arrays, all of which may be scattered throughout storage (Key term: poor spatial locality). Definitely prefer the `std::vector` to the raw pointers to arrays. Using raw pointers for this forces the class to observe the [Rule of three and should observe the Rule of Five](https://en.cppreference.com/w/cpp/language/rule_of_three). If you use `std::vector` in place of the raw pointers the class can observe the Rule of Zero and save you from having to write some potentially tricky code. – user4581301 Mar 02 '19 at 08:09
  • Thanks sir. But i don't know how to use vectors. I am currently trying to learn it and about all the things everyone told me (in comments) that what i am supposed to do with my code. I hope i will soon come up with a answer. Thanks – Cruz Mar 02 '19 at 17:54