gpt4 book ai didi

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

转载 作者:太空狗 更新时间:2023-10-29 23:01:44 25 4
gpt4 key购买 nike

你好 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

任何我的 .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 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
}
}

我遇到的问题是输出。调用 WriteData 函数时,它会写入输出文件,但不会写入它读取的数据。相反,所有写入的都是 {0 1 2 3 4 5 6 7 8} {1 2 3 4 5 6 7 8 9} {etc.} 其中 { 在这里用于表示不同的行数

此外,如果我尝试输出 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 )返回 p[i * _cols + j];,因为它更易于阅读。

关于c++ - 读取使用运算符创建的矩阵,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30384517/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com