Моя задача - перегрузить мои собственные потоковые операторы «<<» и «>>» для чтения и записи значений из и в мою таблицу. Я не уверен, как реализовать это с моими SaveGrid и LoadGrid. Я прочитал несколько руководств по перегрузке операторов, и это то, что я получил до сих пор.
Я уже реализовал друзей класса, но я не уверен, как реализовать их в моей сетке. cpp.
friend ostream& operator<<(ostream& out, Grid& grid);
friend istream& operator>>(istream& in, Grid& grid);
Ища некоторые предложения о том, как я могу реализовать это, чтобы я мог читать и использовать имеющиеся у меня методы, используя мои перегруженные операторы с чем-то вроде cout << grid и cin >> grid, я извиняюсь, что Я не очень четко сформулировал это, но любой совет вообще приветствуется.
Сетка. cpp
#include "Grid.h"
#include<iostream>
#include<fstream>
using namespace std;
void Grid::LoadGrid(const char filename[])
{
ifstream newStream;
newStream.open(filename);
for (int y = 0; y < 9; y++)
{
for (int x = 0; x < 9; x++)
{
newStream >> m_grid[y][x];
}
}
}
void Grid::SaveGrid(const char filename[])
{
ofstream newStreamOut(filename);
for (int y = 0; y < 9; y++)
{
for (int x = 0; x < 9; x++)
{
newStreamOut << m_grid[y][x] << " ";
}
newStreamOut << endl;
}
}
ostream& operator<<(ostream& out, Grid& grid)
{
grid.SaveGrid(out);
return out;
}
istream& operator>>(istream& in, Grid& grid)
{
grid.LoadGrid(in);
return in;
}
Сетка.h
#pragma once
#include<ostream>
using namespace std;
class Grid
{
public:
Grid() {};
~Grid(){};
friend ostream& operator<<(ostream& out, Grid& grid);
friend istream& operator>>(istream& in, Grid& grid);
void LoadGrid(const char filename[]);
void SaveGrid(const char filename[]);
private:
int m_grid[9][9];
};
основной. cpp
#include <iostream>
#include "Grid.h"
using namespace std;
int main(int args, char** argv)
{
Grid grid;
grid.LoadGrid("Grid1.txt");
grid.SaveGrid("OutGrid.txt");
system("pause");
}