读取使用运算符创建的矩阵
Reading a matrix created using an operator
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];
,因为它使它更容易阅读。
相关文章:
- 如何使用新运算符跟踪在循环中创建的 QLabel
- 如何创建运算符重载?
- 为什么在C++中使用关系运算符创建的模板函数不能对字符串正常工作?
- 如何在C++中使用 new 运算符创建对动态创建的数组的引用?
- 使用新运算符C++创建多维数组的简单方法
- 为字典cpp创建[]=运算符
- 创建变量之间的运算符排列
- 如何从模板类重载创建的指针对象上的运算符?
- 如何为 Set 创建运算符 []?
- 创建指针是否超过非数组指针的末尾,而不是从 C++17 中的一元运算符和未定义的行为派生?
- std::map<int, A> 运算符 [] 需要使用空构造函数创建 A
- 为 2D 向量类创建标量乘法运算符
- 在 rxcpp 中创建自定义运算符
- 运算符 () 括号是否会在函数调用中创建的对象上调用
- C++创建新的运算符并使当前运算符重载
- 如何为= 0创建C 分配运算符
- C++的新增功能:创建 2D 数组并具有正确的赋值运算符:分割错误
- 三元运算符为C++中返回的引用创建临时
- 在 c++ 中为类创建 [][] 运算符
- 在类中创建运算符+而不声明太多不必要的变量的最佳方法