【发布时间】:2019-05-04 08:09:43
【问题描述】:
我有一个模板矩阵,但我不知道如何阅读我的“matrix1”txt 文件。我需要如何定义“open1”方法?这是我的类和我的文件。 在文件中,前两个数字是矩阵的行和列。
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 & other);
void setMatrixElement(unsigned, unsigned, T);
void delMatrix();
T getElement(unsigned = 0, unsigned = 0);
unsigned getRow()const { return row; }
unsigned getColumn()const { return column; }
bool open1();
};
这是我的文件
3 3
26.062000 16.600000 24.900000 49.800000 0.000000 1.000000 2.000000
4.000000 5.000000
编辑:
这是我的新代码,但我无法构建解决方案,也不知道如何处理错误:抛出异常:读取访问冲突。 this->矩阵是 0x1110112。”
template <class T>
bool Matrix<T> ::open1()
{
ifstream myfile("matrix1.txt");
if (!myfile)
{
cout << "Error with fileopening" << 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;
}}
【问题讨论】:
-
你尝试了什么?
-
我把它写成答案 – 如果你想对你的问题进行补充,请编辑你的问题。答案是为了答案。
-
这是我的文件——不同行中的数字应该是什么意思?
-
矩阵的元素
-
Matrix<T> M;与open1功能无关。删除它并将该函数中的 allM.anything替换为anything。