7

game.h

#ifndef GAME_H
#define GAME_H
#include <string>
#include <iostream>
#include "piece.h"

using namespace std;

class Game
{
    private:
        string white;
        string black;
        string title;
        istream* in;
        ostream* out;
    public:
        Game();
        Game(istream&, ostream&);
        void display(Colour, short);
};

#endif

game.cpp

#include <iostream>
#include <string>
#include <sstream>
#include "game.h"
#include "board.h"
#include "piece.h"

using namespace std;

Game::Game()
{
    //nothing
}

Game::Game(istream& is, ostream& os)
{
    in = is;
    out = os;
}

void Game::display(Colour colour, short moves)
{
    //out << "a";
}

I'm trying to use the istream and ostream in other parts of my class but I can't because g++ won't let me reference is to in. Any ideas?

Steven
  • 1,061
  • 2
  • 10
  • 14

4 Answers4

7

You simply want a reference variable, not a pointer.

class Game
{
    private:
        ...
        istream& in;
        ostream& out;
    public:
        Game(istream&, ostream&);
};

Game::Game(istream& is, ostream& os)
    : in( is ),
      out( os )
    { }

The existing code compiles because of a couple language quirks:

  • istream / ostream are convrtible to void* to allow you to check their error status as in

      if( in ) { do_something( in ); }
    
  • your compiler apparently allowed void* to be converted to ostream* (I believe in error, and you should at least get a warning from this).

Potatoswatter
  • 134,909
  • 25
  • 265
  • 421
  • I also used help from http://stackoverflow.com/questions/366955/obtain-a-stdostream-either-from-stdcout-or-stdofstreamfile/366969#366969 – Steven Apr 04 '11 at 01:29
4

You should deference the pointer:

*out << "a";

For more convenient use, to not deference the pointers each time, and for more readability you can use references instead of pointers.

class Game
{
    // ...
    std::istream& in;    // notice explicit namespace std::
    std::ostream& out;
    // ...
};

Then you can write:

out << "a";

Plus, it is not a good habit to do so:

using namespace std;

This way you are exposing the names of std namespace.

Hovhannes Grigoryan
  • 1,151
  • 1
  • 8
  • 11
2

is is a reference not a pointer, therefore if you want to store a pointer you need to use the address operator in = &is;

But please realize that is can cease to exist immediately after the method call, therefore you can easily end up with an invalid pointer. Make sure that you at least document this fact.

Šimon Tóth
  • 35,456
  • 20
  • 106
  • 151
  • out << "a" gives me: game.cpp: In member function ‘void Game::display(Colour, short int)’: game.cpp:23: error: invalid operands of types ‘std::ostream*’ and ‘const char [2]’ to binary ‘operator<<’ – Steven Apr 03 '11 at 10:44
  • @Steven Because `in` IS a pointer. Therefore if you want to use the stream it is pointing to, you need to use the dereference operator `*out` – Šimon Tóth Apr 03 '11 at 10:46
  • @Let_Me_Be Thanks, but now it gives a Segmentation Fault at that line. – Steven Apr 03 '11 at 10:53
  • I'm currently using cout as the ostream btw – Steven Apr 03 '11 at 10:54
  • ah, I see once that method call finishes is ceases to exist, any way around this? – Steven Apr 03 '11 at 10:55
1

If you store pointers, you need to dereference them, like *in or *out << ....

Bo Persson
  • 90,663
  • 31
  • 146
  • 203