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;
}
}