C ++:ifstream作为参数 [英] C++: ifstream as a parameter

查看:187
本文介绍了C ++:ifstream作为参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

函数原型:

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

以及主要功能中的区域:

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

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

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?

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

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

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

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.

这篇关于C ++:ifstream作为参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆