Reading a matrix created using an operator

Hi stackoverflow community. I need help with a bit of code (I'm new to C ++, so be careful). I am trying to use operator () to create a matrix, store data from an input file, and then write to an output file. The code below is a bit simplified. The header file looks like this:

//Data Header File

#ifndef Data_h
#define Data_h

#include <iostream>

using namespace std;

class Data
{
private:
    int d_elems;
    int rows_, cols_;
    int dataRows;
    int *p;
public:
    //Constructor
    femData();
    femData(int Row, int Col);

    //Copy Constructor
    //femData(const int d_elems);

    //Destructor
    virtual ~femData();

    //Operator
    int& operator() (int Rows, int Cols);

    //Functions
    void readData(istream &inp);  //Read Data from Input File
    void writeData(ostream &out);  //Write Data from Output File
};
#endif

      

Any my .cpp file:

//.cpp file
#include "stdafx.h"
#include "Data.h"
#include <fstream>
#include <string>
#include <iostream>
#include <iomanip>

using namespace std;

Data::Data() {}  //Blanket Constructor

Data::Data(int Row, int Col) //Matrix Constructor
    : rows_ (Row), cols_ (Col)
{
    if (Row == 0 || Col == 0)
    {
        cout << "\nMatrix is Zero..." << endl;
        system("pause");
        exit(0);
    }
    p = new int[Row * Col];
}

int& Data::operator()(int Rows, int Cols)  //Operator for Matrix
{
    if (Rows >= rows_ || Cols >= cols_)
    {
        cout << "\nMatrix subscript out of bounds\n";
        system("pause");
        exit(0);
    }
    return p[cols_ * Rows + Cols];
}

Data::~Data() { /*delete[] p;*/}  //Destructor

void Data::readData(istream &inp)
{
    inp >> dataRows;
    int e_id;
    //Data (dataRows, 10);  //How would I call this constructor?
    rows_ = dataRows;
    cols_ = 10;

    for (int i = 0; i < dataRows; i++)
    {
        inp >> e_id;

        if ((e_id - 1) != i)
        {
            cout << "\nError Reading Data..." << endl;
            cout << "Program Will End\n!" << endl;
            system("pause");
            exit(0);
        }

        (*this)(i, 0) = d_eid;

        for (int j = 1; j < 10; j++)
        {
            inp >> (*this)(i, j);
        }
    }

    void femData::writeData(ostream & out)
    {
        //Output Info
        out << setfill('-') << setw(90) << "-" << endl;
        out << setfill(' ') << setw(34) << " Matrix Information " << endl;
        out << setfill('-') << setw(90) << "-" << "\n\n" << endl;
        out << setfill(' ');
        out << setw(10) << "ID";
        out << setw(10) << "Data 1";
        out << setw(10) << "Data 2";
        out << setw(10) << "Data 3";
        out << setw(10) << "Data 4";
        out << setw(10) << "Data 5";
        out << setw(10) << "Data 6";
        out << setw(10) << "Data 7";
        out << setw(10) << "Data 8" << endl;

        for (int i = 0; i < dataRows; i++)
        {
            out << setw(7) << ((p + i) + 0);
            out << setw(10) << ((p + i) + 1);
            out << setw(10) << ((p + i) + 2);
            out << setw(10) << ((p + i) + 3);
            out << setw(10) << ((p + i) + 4);
            out << setw(10) << ((p + i) + 5);
            out << setw(10) << ((p + i) + 6);
            out << setw(10) << ((p + i) + 7);
            out << setw(10) << ((p + i) + 8) << endl;
            //Note ((p + i) + 8) is omitted
        }
    }

      

The problem I am facing is with the exit. When the WriteData function is called, it writes the output file, but does not write the data that it reads. Instead, whatever is written {0 1 2 3 4 5 6 7 8} {1 2 3 4 5 6 7 8 9} {etc.}

is where{is used here to denote different rows}

Also, if I try to output d_elems(i,0)

instead ((d_elems + i) + 0)

, the compiler will tell me that I need a pointer to the function type.

Any help would be greatly appreciated, thanks.

+3


source to share


1 answer


Yours is readData

wrong. You read the data into a local object Data d_data(dataRows,10);

, which is then destroyed at the end of the function. You are not filling in the details of your current instance. You should read straight into the `p '

inp >> p[i * rows_ + j];

      

or use operator()

which you defined in the current instance like



inp >> (*this)(i,j); // this is preferable

      

Side problem: there is int *p;

no declaration in the class file header p

.

Side problem 2: yours is int& Data::operator()(int Rows, int Cols)

confusing, try using int& Data::operator()(int i, int j)

and return p[i * _cols + j];

as it makes it easier to read.

+1


source







All Articles