【问题标题】:Overloading ostream operator to write matrix in file重载 ostream 运算符以在文件中写入矩阵
【发布时间】:2017-07-03 12:57:32
【问题描述】:

我想使用重载的 ostream

class matrix
{
private:
    int i;
    int j;
    int ** mrx;

public:
    matrix(); // Konstruktor I
    matrix (int a, int b); // Konstruktor II
    ~matrix (); // Destruktor
    void fill_data (); // fill matrix
    void print (); // print matrix
    bool ToFile(string filename,matrix &m); // MY PROBLEM!!!
    bool FromFile(string filename); // reading from file
    friend ostream & operator << (ostream &stream, matrix & m);
    };

matrix::matrix()
{
i = j = 0;
mrx = NULL;
}

matrix::matrix(int a, int b) //1
{
i = a;
j = b;

mrx = new int * [i];

for (int x = 0; x < i; x++)
    mrx[x] = new int [j];

for (int x = 0; x < i; x++)
    for (int z = 0; z < j; z++)
        mrx[x][z] = 0;
}


matrix::~matrix ()
{
for (int x = 0; x < i; x++)
    delete[] mrx[x];
delete[] mrx;
mrx = NULL;
}


ostream& operator << (ostream& stream, matrix &m)
{
for(int x = 0; x < m.i; x++){
    for(int z = 0; z < m.j; z++)
    cout << m.mrx[x][z] << " ";
cout << endl;
}
return stream;
} 

bool matrix::ToFile(string filename, matrix &m)
{
ofstream file;
file.open(filename.c_str());

if (file.is_open())
{
    file << i << " " << j << "\n";
    /*for (int x = 0; x < i; x++)
    {
        for (int z = 0; z < j; z++)
        file << mrx[x][z] << " ";

        file << "\n";
    }
    */

    file << &mrx;

    file.close();

    return true;
}
else
{
    cout << "Failed" << endl;

    return false;
}
}

int main ()
{
matrix A(4,4);

A.print();
A.fill_data();
cout << "_____________ Matrix A ______________" << endl;
A.print();
cout << "_____________________________________" << endl;
cout << endl;


A.ToFile("testfile.txt", A);


matrix C(6,6);

C.FromFile("testfile.txt");


C.print();
cout << A << endl << endl << C;
return 0;
}

【问题讨论】:

  • 请缩进您的代码。您正在获取矩阵的地址(使用 operator&)并将其传递给流 operatorfile << mrx。
  • 你应该添加一些body函数,因为我必须添加一些空body

标签: c++ file matrix operator-overloading


【解决方案1】:

您不应该通过引用给出 mrx,该代码可以工作并将矩阵保存在文件中,但您必须记住,该函数应该有一些主体(因为您在没有主体的情况下给出了代码)

你的operator&lt;&lt;不好用,他应该把数据保存到std::ostream stream你必须要std::cout,现在可以正常工作了。

#include <iostream>
#include <fstream>
using namespace std;
class matrix
{
private:
    int i;
    int j;
    int ** mrx;

public:
    matrix(); // Konstruktor I
    matrix(int a, int b); // Konstruktor II
    ~matrix(); // Destruktor
    void fill_data() {}; // fill matrix
    void print(){}; // print matrix
    bool ToFile(string filename, matrix &m); // MY PROBLEM!!!
    bool FromFile(string filename) { return 0; }; // reading from file
    friend ostream & operator << (ostream &stream, matrix & m);
};

matrix::matrix()
{
    i = j = 0;
    mrx = NULL;
}

matrix::matrix(int a, int b) //1
{
    i = a;
    j = b;

    mrx = new int *[i];

    for (int x = 0; x < i; x++)
        mrx[x] = new int[j];

    for (int x = 0; x < i; x++)
        for (int z = 0; z < j; z++)
            mrx[x][z] = 0;
}


matrix::~matrix()
{
    for (int x = 0; x < i; x++)
        delete[] mrx[x];
    delete[] mrx;
    mrx = NULL;
}


ostream& operator << (ostream& stream, matrix &m)
{
    for (int x = 0; x < m.i; x++) {
        for (int z = 0; z < m.j; z++)
            stream << m.mrx[x][z] << " ";
        stream << endl;
    }
    return stream;
}

bool matrix::ToFile(string filename, matrix &m)
{
    ofstream file;
    file.open(filename);

    if (file.is_open())
    {
        file << i << " " << j << "\n"; 
        file << m;

        file.close();

        return true;
    }
    else
    {
        cout << "Failed" << endl;

        return false;
    }
}

int main()
{
    matrix A(4, 4);

    A.print();
    A.fill_data();
    cout << "_____________ Matrix A ______________" << endl;
    A.print();
    cout << "_____________________________________" << endl;
    cout << endl;


    A.ToFile("testfile.txt", A);


    matrix C(6, 6);

    C.FromFile("testfile.txt");


    C.print();
    cout << A << endl << endl << C;
    return 0;
}

【讨论】:

    【解决方案2】:

    因为file &lt;&lt; &amp;mrx 你写的是mrx 的地址。这是一个指向指向int 的指针的指针。你实际上并没有写matrix对象m

    file << m;
    

    【讨论】:

    • 不,抱歉,它不会改变任何东西。 :( 我也试过file &lt;&lt; m.mrx .... 仍然是大小 i 和 j (4,4) 和地址
    • @VitaCola2010 这不应该是因为mrx 是一个指针。你想写m 对吗?更新了我的答案。
    • 是的,我想使用重载 ostream friend ostream & operator << (ostream &stream, matrix & m); 将矩阵 m 写入文件是不正确的。
    • @VitaCola2010 使用您当前的代码(如问题所示),您甚至不会调用重载的operator&lt;&lt; 函数。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-07-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-10-10
    • 1970-01-01
    • 2012-05-04
    相关资源
    最近更新 更多