我正在尝试制作一个程序,该程序既可以创建.txt文件,又可以读取该文件以创建用于Linux的tic-tac-toe的输出。但是,我的功能之一不是初始化它的第一个参数,即来自相关文件的输入流,以便创建所需的输出。

函数原型:

void loadSquaresFromStream(  ifstream inStream, char statusSquare[], int currGame, int
row, int column, int mark, int player, int games_Left );

// This function reads from the file 'games.txt' and uses it to update the array
// used to output each game.


功能头部和身体:

void loadSquaresFromStream(  ifstream inStream, char statusSquare[], int currGame, int
row, int column, int mark, int player, int games_Left ) {
  while ( currGame == 1 ) {
    inStream >> row >> column;
    if ( row == 1 && column == 1 && statusSquare[1] == ' ' ) {
      statusSquare[1] = mark;
      player++;
    }
    else if ( row == 1 && column == 2 && statusSquare[2] == ' ' ) {
      statusSquare[2] = mark;
      player++;
    }
    else if ( row == 1 && column == 3 && statusSquare[3] == ' ' ) {
      statusSquare[3] = mark;
      player++;

....
....
    else if ( row == 0 && column == 0 ) {
    currGame = 0;
    }
    else {
      currGame = 0;
      games_Left = 0;
    }
  }
}


并且该区域中的主要功能是:

int main() {
....
....
 else if ( option == '2' ) { //line 189
    cout << " Checking for file 'games.txt' in current directory... " << endl;
    inStream.open ( "games.txt" );
    if ( inStream.fail( )) {
    cout << " ERROR: File 'games.txt' was not found.  Please re-execute this program and play a game to create the file before using this feature. " << endl;
    exit(1);
    }
    else {
      cout << " File 'games.txt' found.  Displaying output now. " << endl << endl;
    }
    system( "cls" );
    while ( games_Left == 1 ) {
      cout << "Board Positions for Game " << games << ": " << endl;
      loadSquaresFromStream( inStream, statusSquare, currGame, row, column, mark, player, games_Left);  //line 202
....
....
}


这是我尝试从cygwin和code :: blocks进行编译时遇到的错误语法。

In file included from /usr/lib/gcc/i686-pc-cygwin/4.8.2/include/c++/ios:42:0,
                 from /usr/lib/gcc/i686-pc-cygwin/4.8.2/include/c++/ostream:38,
                 from /usr/lib/gcc/i686-pc-cygwin/4.8.2/include/c++/iostream:39,
                 from jebecker_assignment01.cpp:16:
/usr/lib/gcc/i686-pc-cygwin/4.8.2/include/c++/bits/ios_base.h: In copy constructor ‘std::basic_ios<char>::basic_ios(const std::basic_ios<char>&)’:
/usr/lib/gcc/i686-pc-cygwin/4.8.2/include/c++/bits/ios_base.h:786:5: error: ‘std::ios_base::ios_base(const std::ios_base&)’ is private
     ios_base(const ios_base&);
     ^
In file included from /usr/lib/gcc/i686-pc-cygwin/4.8.2/include/c++/ios:44:0,
                 from /usr/lib/gcc/i686-pc-cygwin/4.8.2/include/c++/ostream:38,
                 from /usr/lib/gcc/i686-pc-cygwin/4.8.2/include/c++/iostream:39,
                 from jebecker_assignment01.cpp:16:
/usr/lib/gcc/i686-pc-cygwin/4.8.2/include/c++/bits/basic_ios.h:66:11: error: within this context
     class basic_ios : public ios_base
           ^
In file included from jebecker_assignment01.cpp:17:0:
/usr/lib/gcc/i686-pc-cygwin/4.8.2/include/c++/fstream: In copy constructor ‘std::basic_ifstream<char>::basic_ifstream(const std::basic_ifstream<char>&)’:
/usr/lib/gcc/i686-pc-cygwin/4.8.2/include/c++/fstream:427:11: note: synthesized method ‘std::basic_ios<char>::basic_ios(const std::basic_ios<char>&)’ first required here
     class basic_ifstream : public basic_istream<_CharT, _Traits>
           ^
In file included from /usr/lib/gcc/i686-pc-cygwin/4.8.2/include/c++/ios:43:0,
                 from /usr/lib/gcc/i686-pc-cygwin/4.8.2/include/c++/ostream:38,
                 from /usr/lib/gcc/i686-pc-cygwin/4.8.2/include/c++/iostream:39,
                 from jebecker_assignment01.cpp:16:
/usr/lib/gcc/i686-pc-cygwin/4.8.2/include/c++/streambuf: In copy constructor ‘std::basic_filebuf<char>::basic_filebuf(const std::basic_filebuf<char>&)’:
/usr/lib/gcc/i686-pc-cygwin/4.8.2/include/c++/streambuf:802:7: error: ‘std::basic_streambuf<_CharT, _Traits>::basic_streambuf(const std::basic_streambuf<_CharT, _Traits>&) [with _CharT = char; _Traits = std::char_traits<char>]’ is private
       basic_streambuf(const basic_streambuf& __sb)
       ^
In file included from jebecker_assignment01.cpp:17:0:
/usr/lib/gcc/i686-pc-cygwin/4.8.2/include/c++/fstream:72:11: error: within this context
     class basic_filebuf : public basic_streambuf<_CharT, _Traits>
           ^
/usr/lib/gcc/i686-pc-cygwin/4.8.2/include/c++/fstream: In copy constructor ‘std::basic_ifstream<char>::basic_ifstream(const std::basic_ifstream<char>&)’:
/usr/lib/gcc/i686-pc-cygwin/4.8.2/include/c++/fstream:427:11: note: synthesized method ‘std::basic_filebuf<char>::basic_filebuf(const std::basic_filebuf<char>&)’ first required here
     class basic_ifstream : public basic_istream<_CharT, _Traits>
           ^
jebecker_assignment01.cpp: In function ‘int main()’:
jebecker_assignment01.cpp:202:98: note: synthesized method ‘std::basic_ifstream<char>::basic_ifstream(const std::basic_ifstream<char>&)’ first required here
    loadSquaresFromStream( inStream, statusSquare, currGame, row, column, mark, player, games_Left);
                                                                                                  ^
jebecker_assignment01.cpp:31:6: error:   initializing argument 1 of ‘void loadSquaresFromStream(std::ifstream, char*, int, int, int, int, int, int)’
 void loadSquaresFromStream(  ifstream inStream, char statusSquare[], int currGame, int row, int column, int mark, int player, int games_Left );
      ^


谁能弄清楚该程序出了什么问题?

编辑:将ifstream更改为通过引用进行调用后,在尝试编译时得到了以下信息:

/tmp/ccM1q9XN.o:jebecker_assignment01.cpp:(.text+0x416): undefined reference to `displayActiveBoard()'
/tmp/ccM1q9XN.o:jebecker_assignment01.cpp:(.text+0xaf5): undefined reference to `checkActiveGameStatus()'
/tmp/ccM1q9XN.o:jebecker_assignment01.cpp:(.text+0xb17): undefined reference to `displayActiveBoard()'
/usr/lib/gcc/i686-pc-cygwin/4.8.2/../../../../i686-pc-cygwin/bin/ld: /tmp/ccM1q9XN.o: bad reloc address 0x0 in section `.ctors'
collect2: error: ld returned 1 exit status


这是否与我编写其他函数的方式有关,还是因为'games.txt'还不存在?

最佳答案

ifstream对象无法复制。而是通过引用传递。

关于c++ - C++:ifstream作为参数,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/22188476/

10-13 05:53