简体   繁体   中英

C++: ifstream as a parameter

I'm trying to make a program that will be able to both create a .txt file and reads from it to create output for tic-tac-toe meant for Linux. However, one of my functions is not initializing it's first parameter, the input stream from the file in question, in order to create the output needed.

The function prototype:

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.

The function head and body:

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;
    }
  }
}

And the area in the main function:

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
....
....
}

This is the error syntax I got while trying to compile from cygwin and 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 );
      ^

Could anyone figure out what's going wrong with the program?

EDIT: After changing ifstream to a call by reference, I got this when I tried to compile:

/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

Is this related to how I wrote my other functions, or is it because 'games.txt' does not exist yet?

您不能按值传递ifstream :它是不可复制的。

ifstream objects can't be copied. Pass by reference instead.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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