简体   繁体   English

尝试实现我自己的 << 和 >> 流操作符来在我的网格 object 上操作

[英]Trying to implement my own << and >> streaming operators to operate on my Grid object

My task is to overload my own "<<" and ">>" streaming operators to read and write values from and into my Grid.我的任务是重载我自己的“<<”和“>>”流操作符,以便在我的网格中读取和写入值。 I am not sure how to implement this with my SaveGrid and LoadGrid.我不确定如何使用我的 SaveGrid 和 LoadGrid 来实现这一点。 I've read multiple tutorials on overloading operators and this is what I got so far.我已经阅读了多个关于重载运算符的教程,这就是我到目前为止所得到的。

I have already implemented friends of the class but I am not sure how to implement them in my grid.cpp.我已经实现了 class 的朋友,但我不确定如何在我的 grid.cpp 中实现它们。

friend ostream& operator<<(ostream& out, Grid& grid);
friend istream& operator>>(istream& in, Grid& grid);

Looking for some suggestions on how I can implement this so I can read in and out with my existing methods using my overloaded operators with something like cout << grid and cin >> grid, I apologise that I have not articulated this very well but any advice at all is appreciated.寻找一些关于如何实现这一点的建议,以便我可以使用我的重载运算符和 cout << grid 和 cin >> grid 等现有方法读取和读取,我很抱歉我没有很好地表达这一点,但是任何非常感谢您的建议。

Grid.cpp网格.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;
    }

Grid.h网格.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];
};

main.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");
}

Move the logic from your LoadGrid and SaveGrid functions (all the stuff that operates on the filestream that you open) into your >> and << functions.将逻辑从您的LoadGridSaveGrid函数(在您打开的文件流上运行的所有内容)移到您的>><<函数中。 Also add const for the objects you're not supposed to change.还要为您不应该更改的对象添加const

    ostream& operator<<(ostream& out, const Grid& grid)  // grid should be const
    {
        for (int y = 0; y < 9; y++)
        {
            for (int x = 0; x < 9; x++)
            {
                out << grid.m_grid[y][x] << " ";

            }
            out << endl;
        }
        return out;
    }

Then your LoadGrid and SaveGrid functions are just helpers that call the stream operators, but take care of opening the filestream.然后您的LoadGridSaveGrid函数只是调用 stream 运算符的助手,但要注意打开文件流。

    void Grid::SaveGrid(const char filename[]) const   // *this should be const
    {
        ofstream newStreamOut(filename);

        newStreamOut << *this;
    }

Except now you can output your Grid to any other ostream you want:除了现在你可以 output 你的Grid到你想要的任何其他ostream

std::cout << my_grid << "\n";

Afterwards, do the same for the istream side.之后,对istream端执行相同操作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM