0

I am working on a project that involves reading in a SparseMatrix using the >> operator and I had a question.

In our example code, it wants us to read a SparseMatrix in from a file by using the input operator, an example being:

SparseMatrix m;

infile >> m;

My question is when I am overloading the input operator for my SparseMatrix class, can I do something like this:

std:: istream& operator>>(std::istream &in, SparseMatrix &m) {
    int rowIn, colIn;
    double dataIn;
    string line;
    while(in >> line) {
        if(line.sizeof() == 3) {
            cin >> rowIn;
            cin >> colIn;
            cin >> dataIn;
            SparseMatrix(rowIn, colIn, dataIn);
        }
        else {
            SparseMatrix();
        }
    }
    return in;
}

Or can I not do it that way? I can include any extra code needed, just ask. I just tried to keep it short so this isn't a wall of text. Any help would be appreciated :)

Mykola
  • 3,343
  • 6
  • 23
  • 39
  • The signature is not the right one. Beside this, you should know the file format. – zdf Feb 25 '16 at 17:54
  • In what way? I'm overloading the operator for my SparseMatrix class. And the file format is merely a row int, col int, and a double containing the value in it – Adam Alred Feb 25 '16 at 18:05
  • I do not see any `std::istream` in your function. – zdf Feb 25 '16 at 18:14
  • Oh, I see, I miswrote that, my fault. – Adam Alred Feb 25 '16 at 18:19
  • I would add a get( std::istream& ) method and call it from overloaded operator>>. – zdf Feb 25 '16 at 18:22
  • Just edited it with my new input function. Let me know what you think. – Adam Alred Feb 25 '16 at 18:36
  • I cannot be of any help since there are too many basic things that you do not understand. You should read about _sizeof, constructors/destructors, streams..._ Leaving the basics aside, here it is a solution to your problem: **1.** You have to add a `get` or, better `SparseMatrix::read` method to your matrix class. The signature should be `void read( std::istream& is )`. The body should read from stream to `EOF` and put the values it reads into matrix. – zdf Feb 25 '16 at 21:43
  • **2.** You do not clearly explain the format of your file, but from what you wrote it looks like each non-zero value is stored as row/column/value. To read this input you don’t need to read the line first. Try this: `while( is >> row >> col >> data ) { …` **3.** Once you read the data, you have to store it in the matrix. I assume that you already have a `set_at( const int row, const int col, const double data)` method. So, the body of `while` should be a call to this method: `{ m.set_at( row, col, data ); }` – zdf Feb 25 '16 at 21:44
  • **4.** Now, you have a method to read the matrix from a stream. To make it work with `>>` you have to define a global overloaded operator: `std::istream& operator>>( std::istream& is, SparseMatrix& sm ) { sm.read( is ); return is; }` **5.** You can now use `>>` with your class: `std::ifstream is( “test.txt” ); … SparseMatrix m; is >> m;` – zdf Feb 25 '16 at 21:55
  • I figured it out. Thanks anyway. – Adam Alred Feb 26 '16 at 19:19

0 Answers0