读取使用运算符创建的矩阵

Reading a matrix created using an operator

本文关键字:创建 运算符 读取      更新时间:2023-10-16

stackoverflow社区你好。我需要一些代码的帮助(我是一个新的c++,所以温柔)。我试图使用operator()来创建一个矩阵,从输入文件存储数据,然后写入输出文件。下面的代码进行了一些简化。头文件如下:

//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

任何my .cpp文件:

//.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 boundsn";
        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 Endn!" << 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) << "-" << "nn" << 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
        }
    }

我遇到的问题是输出。当WriteData函数被调用时,它会写入一个输出文件,但不会写入它所读取的数据。而是写成{0 1 2 3 4 5 6 7 8} {1 2 3 4 5 6 7 8 9} {etc.},其中{is used here to denote different rows}

此外,如果我试图输出d_elems(i,0)而不是((d_elems + i) + 0),编译器告诉我需要一个指向函数类型的指针。

您的readData是错误的。在本地对象Data d_data(dataRows,10);中读取数据,然后在函数结束时销毁该对象。您没有填写当前实例上的数据。你需要直接读入' p'

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

或使用您在当前实例上定义的operator(),如

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

附带问题:您在类头文件int *p;中缺少p的声明。

附带问题2:你的int& Data::operator()(int Rows, int Cols)是混乱的,尝试使用int& Data::operator()(int i, int j),和return p[i * _cols + j];,因为它使它更容易阅读。