0

I get read access violation while I try to build the solution.I have a matrix class and I would like to read a matrix from txt file(to work with).With debugging there were no errors but when I tried to run the program I got "exception thrown message".The message appears in open1 method at "matrix [i][j]"-s line.

{
template <class T>
bool Matrix<T> ::open1()


{


    ifstream myfile("matrix1.txt");

    if (!myfile)
    {
        cout << "Error in file opening" << endl;

        return false;

    }
    myfile >> row;
    myfile >> column;
    for (int i = 0; i < row; i++)
    {
        for (int j = 0; j < column; i++)
        {
            myfile >> matrix[i][j];
        }
    }




    myfile.close();                     
    return true;
}

Here is the class

{
template<class T = double>
class Matrix
{
private:
unsigned row;
unsigned column;
T **matrix;
template<class OUTP>
friend std::ostream& operator<<(std::ostream&, const Matrix<OUTP>&);
template<class INP>
friend std::istream& operator>>(std::istream&, Matrix<INP>&);
public:
Matrix(unsigned = 0, unsigned = 0);
~Matrix();
Matrix(const Matrix<T>&);
void setMatrixElment(unsigned, unsigned, T);
void delMatrix();
unsigned getRow()const { return row; }
unsigned getColumn()const { return column; }
T getElement(unsigned = 0, unsigned = 0);
Matrix<T>& operator=(const Matrix<T>&);
Matrix<T> operator+(const Matrix<T>&);
Matrix<T> operator-(const Matrix<T>&);

Matrix <T>inverz();
bool open1();
bool open2();

Matrix<T> operator*(const double&);
Matrix<T> operator*(const Matrix<T>&);
T determinant() const;
};

}

My txt file is this

{
  3 3
26.062000 16.600000 24.900000 
49.800000 0.000000 1.000000 
2.000000 4.000000 5.000000

If I allocate memory it still doesn't read the elements.(I defined the complex class well, with the type there wasn't problem) {

template <class T>
bool Matrix<T> ::open1()


{


    ifstream myfile("matrix1.txt");

    if (!myfile)
    {
        cout << "Error in file opening" << endl;

        return false;

    }

    Matrix<Complex> b;
    myfile >> b.row;
    myfile >> b.column;
    b.matrix = new T*[b.row];  
    for (unsigned i = 0; i < b.row; i++)
    {
        b.matrix[i] = new T[b.column];
    }
    for (int j = 0; j < b.row; j++)
    {
        for (int k = 0; k< b.column; k++)
        {
            myfile >> b.matrix[j][k];
        }
    }




    myfile.close();                     
    return true;
}
}
jacky
  • 73
  • 7
  • What does your debugger tell you? – Daniel Kamil Kozar May 11 '19 at 21:48
  • "still doesn't read the elements" - that sounds like a separate problem, you should ask another question and include all the code related to reading the file – kmdreko May 11 '19 at 21:56
  • It shows warnings:Warning C4018 '<': signed/unsigned mismatch at {for (int j = 0; j < b.row; j++)} and {for (int k = 0; k < b.row; k++)} – jacky May 11 '19 at 22:00

1 Answers1

1

I believe that you do not allocate memory for matrix anywhere, since you when you read the dimensions of the matrix (row and column), you rush into parsing the values of the matrix from the file.

You need to dynamic allocate memory in your case, like this:

myfile >> row;
myfile >> column;

matrix = new T*[row];
for(int i = 0; i < row; ++i)
    matrix[i] = new T[column];

for (int i = 0; i < row; i++)
{
    for (int j = 0; j < column; i++)
    {
        myfile >> matrix[i][j];
    }
}
gsamaras
  • 71,951
  • 46
  • 188
  • 305
  • Warning: Do not actually use this code for memory allocation. Instead use a smart pointer. And reduce down to only two allocations (for improved contiguity) as shown here: https://stackoverflow.com/a/29375830/103167 – Ben Voigt May 11 '19 at 22:20
  • 1
    I don’t fully agree with the comment above. Honestly how pointers are used in the above code is readable and natural to the language. However when you do this make sure you utilize the RAII pattern (Resource Acquisition is Initialization). Simply try to keep anything that needs memory at runtime a member variable and do all of your allocating and deleting of the variable in the constructor/deconstructor. Just my two cents – Grant Folgate May 12 '19 at 01:13