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"; }
我正在尝试在我班级的其他部分使用istream和ostream,但我不能因为g不会让我参考进来.有什么想法吗?
解决方法
您只需要一个引用变量,而不是指针.
class Game { private: ... istream& in; ostream& out; public: Game(istream&,ostream&); }; Game::Game(istream& is,ostream& os) : in( is ),out( os ) { }
由于一些语言怪癖,现有代码编译:
> istream / ostream可以使*无效,以允许您检查其错误状态
if( in ) { do_something( in ); }
>你的编译器显然允许将void *转换为ostream *(我相信错误,你应该至少得到一个警告).