C++ - Errors met while designing a Matrix class

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • slizorn
    New Member
    • Jul 2008
    • 34

    C++ - Errors met while designing a Matrix class

    Hi guys,
    well i am learning c++ on my own..
    i am studying it by refering to the book and doin its exercises.
    well i need help sorting out the errors.. and i am not sure wat destructors and constructors are.. plus i am coding using Visual C++ 2008 express edition..
    this program needs me to input a matrix from a file..
    the file called matrix1.txt is as follows:
    <matrix>
    rows = 2
    cols = 2

    1 2
    2 4
    </matrix>
    so we are meant to input the data from the file into the matrix and carry out the operations..

    the code is as follows for Matrix.h
  • slizorn
    New Member
    • Jul 2008
    • 34

    #2
    [code=cpp]
    #pragma once

    class Matrix
    {
    public:
    Matrix(void);
    ~Matrix(void);
    void readMatrixFile( string FILENAME);
    void addMatrix(Matri x m1, Matrix m3);
    void displayMatrix() ;
    void transposeMatrix ();
    void multiplyMatrice s(Matrix m2, Matrix m3);

    private:
    int row;
    int col;
    double m[][];
    };

    [/code]


    the code for Matrix.cpp is as follows

    [code=cpp]
    #include <iostream>
    #include <fstream>
    #include <string>
    #include "Matrix.h"
    using namespace std;


    Matrix::Matrix( void)
    {
    }


    Matrix::~Matrix (void)
    {
    }


    Matrix::readMat rixFile(string FILENAME)
    {
    const string filename(FILENA ME);
    ifstream file(filename.c _str());
    string line;
    if (getline(file, line) && line == "<matrix>")
    {
    if (file >> line /* "rows" */ && file >> line /* "=" */ && file >> row && getline(file,li ne) /* ; */)
    {
    if (file >> line /* "cols" */ && file >> line /* "=" */ && file >> col && getline(file,li ne) /* ; */)
    {
    for (int r = 0; r < row; r++)
    {
    for (int c = 0; c < col; c++)
    {
    int value;
    if (file >> value)
    {
    m[r][c] = value;
    }
    }
    cout << endl;
    }
    }
    }
    }
    }


    Matrix::addMatr ix(Matrix m1, Matrix m3) // needs some modification...
    {
    for(int a = 0; a < row; a++)
    {
    for(int b = 0; b < col; b++)
    {
    m3[a][b]=(m1[a][b] + m2[a][b]);
    }
    }
    }


    Matrix::display Matrix()
    {
    for(int i = 0; i < row; i++)
    {
    cout << "| ";
    for (int j = 0; j < col; j++)
    {
    cout << m3[i][j] << " ";
    }
    cout << "|" << endl;
    }
    }


    Matrix::transpo seMatrix()
    {
    for(int k = 0; k < row; k++)
    {
    for(int j = 0; j < col; j++)
    {
    int save[2][2];
    save[k][j] = m1[k][j];
    m3[k][j] = m1[j][k];
    m3[j][k] = save[k][j];
    }
    }
    }


    Matrix::multipl yMatrices(Matri x m2, Matrix m3) // needs soem modification like addMatrix
    {
    if(m1.col != m2.row)
    {
    cout << "These two matrices cannot be multiplied!!!" << endl;
    }
    else
    {
    for(int i = 0; i < m1.row; i++)
    {
    for(int j = 0; j < m2.col; j++)
    {
    for(int k = 0; k < m2.row; k++)
    {
    m3[i][j] = m3[i][j] + m1[i][k] * m2[k][j];
    }
    }
    }
    }
    }
    [/code]

    and the code for exercise2.cpp is as follows
    [code=cpp]
    #include <iostream>
    #include <fstream>
    #include <string>
    #include "Matrix.h"
    using namespace std;


    int main(int argc, char* argv[])
    {
    Matrix m1, m2 ,m3;
    char theOption;
    string filename1, filename2;

    while(theOption != '5')
    {
    cout << "Please choose one of the following options from the menu below:" << endl;
    cout << "---------------------------------------------------------------" << endl;
    cout << "(1) Add two matrices" << endl;
    cout << "(2) Multiply two matrices" << endl;
    cout << "(3) Take transpose of a matrix" << endl;
    cout << "(4) Display a matrix" << endl;
    cout << "(5) Exit" << endl;
    cout << "---------------------------------------------------------------" << endl;
    cin >> theOption;

    switch(theOptio n)
    {
    case '1':
    cout << "You have chosen option 1!: Add two matices!" << endl;
    cout << "Please enter the two filenames that contain the two input matrices respectively!" << endl;
    cout << "Enter name of 1st file" << endl;
    cin >> filename1;
    cout << "Enter name of 2nd file" << endl;
    cin >> filename2;
    m1.readMatrixFi le(filename1);
    m2.readMatrixFi le(filename2);
    m2.addMatrix(m1 , m3);
    cout << "The two matrices have been added together." << endl;
    cout << "Select (4): Display a matrix to view it." << endl << endl;
    break;

    case '2':
    cout << "You have chosen option 2!: Multiply two matices!" << endl;
    cout << "Please enter the two filenames that contain the two input matrices respectively!" << endl;
    cout << "Enter name of 1st file" << endl;
    cin >> filename1;
    cout << "Enter name of 2nd file" << endl;
    cin >> filename2;
    m1.readMatrixFi le(filename1);
    m2.readMatrixFi le(filename2);
    m1.multiplyMatr ices(m2, m3);
    cout << "The two matrices have been multiplied with each other." << endl;
    cout << "Select (4): Display a matrix to view it." << endl << endl;
    break;

    case '3':
    cout << "You have chosen option 3!: Transpose a matrix!" << endl;
    cout << "Please enter the filename that contains the matrix to be transposed!" << endl;
    cout << "Enter name of file" << endl;
    cin >> filename1;
    m1.readMatrixFi le(filename1);
    m1.transposeMat rix();
    cout << "The matrix has been transposed." << endl;
    cout << "Select (4): Display a matrix to view it." << endl << endl;
    break;

    case '4':
    cout << "You have chosen option 4!: Display a matix!" << endl;
    cout << "Please enter the option 1, 2 or 3 to display the respective matrices!" << endl;
    cin >> whichMatrixToDi splay;
    displayMatrix() ;
    cout << endl;
    break;

    case '5':
    cout << "You have chosen the option to Quit. Goodbye!" << endl;
    break;

    default:
    cout << " Sorry the option chosen is invalid. Please enter a valid option!" << endl << endl;
    break;
    }
    }

    return 0;
    }
    [/code]
    what is wrong with my program?
    thanks in advance.. :D
    Last edited by slizorn; Jul 31 '08, 12:50 PM. Reason: [/code] & [code=cpp]

    Comment

    • arnaudk
      Contributor
      • Sep 2007
      • 425

      #3
      Why do you ask? What happened when you tried to compile and run your code?
      Please enclose your code in [code=cpp] ... [/code] tags.

      Comment

      • slizorn
        New Member
        • Jul 2008
        • 34

        #4
        hi i got lots of errors.
        i tried for a few hours to reduce them..
        i managed to bring it down from abt 120 errors to 43 errors..
        and i tried my best to correct each error..
        but looks like i need more experience or help before i can fix them
        could u please help me spot the errors?
        and teach me how to use the destructos and constructors?
        thanks

        Comment

        • newb16
          Contributor
          • Jul 2008
          • 687

          #5
          Originally posted by slizorn
          hi i got lots of errors.
          i tried for a few hours to reduce them..
          i managed to bring it down from abt 120 errors to 43 errors..
          and i tried my best to correct each error..
          but looks like i need more experience or help before i can fix them
          Is there anything more difficult than missed declaration?

          just a few
          1) Ewe never allocate memory for m[][], it will crash on first run.
          2) in multiplymatrix there is no m1 variable in scope, and m3 elements are left unitialized before ewe add values to them.

          eta-47: error: ISO C++ forbids declaration of `readMatrixFile ' with no
          type
          - so what? add void in front of as in class declaration

          ::transposeMatr ix()
          int save[2][2];
          totaly wrong - use double as temp and I eve don't understand how is it supposed to work
          m3 is not defined as well

          error: no match for 'operator[]' in 'm3[a]'
          Yes. Use m3.m[a] instead - from class methods ewe can access private members of classes of your type.

          Comment

          • slizorn
            New Member
            • Jul 2008
            • 34

            #6
            [QUOTE=newb16][QUOTE=slizorn]hi i got lots of errors.
            i tried for a few hours to reduce them..
            i managed to bring it down from abt 120 errors to 43 errors..
            and i tried my best to correct each error..
            but looks like i need more experience or help before i can fix them
            Is there anything more difficult than missed declaration?

            just a few
            1) Ewe never allocate memory for m[][], it will crash on first run.
            2) in multiplymatrix there is no m1 variable in scope, and m3 elements are left unitialized before ewe add values to them.
            yea about the allocating memory for the m[][]... how do i do it?
            i have a rough idea that i got to use new and delete or somethin like that..
            but not sure how...
            could ya gimme an example for the case where the max size of the matrix is say 10 by 10?
            and the thing with (2).. i want to be able to do m1.multiplyMatr ix(m2, m3);
            but i get wat u mean but how am i to do it by initialize the m3 elements?

            thanks

            Comment

            • newb16
              Contributor
              • Jul 2008
              • 687

              #7
              Originally posted by slizorn
              yea about the allocating memory for the m[][]... how do i do it?
              like m=new (double*)[10]; for(i...) m[i]=new double[10];
              and... for(i...) delete[] m[i]; delete[] m;

              and the thing with (2).. i want to be able to do m1.multiplyMatr ix(m2, m3);
              but i get wat u mean but how am i to do it by initialize the m3 elements?
              thanks
              You need to set each element of m3 to 0 before addition - i.e. before the innermost loop.

              Comment

              • slizorn
                New Member
                • Jul 2008
                • 34

                #8
                Originally posted by newb16
                like m=new (double*)[10]; for(i...) m[i]=new double[10];
                and... for(i...) delete[] m[i]; delete[] m;


                You need to set each element of m3 to 0 before addition - i.e. before the innermost loop.


                errm when i did wat u said for the 1st thing.. i jusut get abt 60 more errors... i dun think that is solving the problem.. if u want i can send u the files.. i cant attach it to this forum for some reason..

                thanks

                Comment

                • newb16
                  Contributor
                  • Jul 2008
                  • 687

                  #9
                  I do not sly neponyal nothing. gugltransleyt some hnyu extradite apologize. Send-a ... in profile button pm - sends nivapros.

                  Comment

                  Working...