将istream传递给函数 [英] Passing istream into a function

查看:69
本文介绍了将istream传递给函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在制作类似于口袋妖怪的游戏类型程序.我们有一个锦标赛类,它跟踪由宠物(其自己的类)组成的几支球队(其自己的类),而不同种类的宠物是CPet的子类.

I am making a game-type program similar to the idea of pokemon. We have a tournament class that keeps track of several teams(its own class) which consists of pets(its own class) with different kinds of pets being subclasses of CPet.

我们正在尝试将文件名传递给main,将文件名从main传递给Tournament类.在锦标赛类中,我们使用以下命令打开文件:

We are attempting to pass a file name into main, from main pass that file name into the Tournament class. In the Tournament class we open the file with:

 14 //Construct a tournament
 15 CTournament::CTournament(const char *Filename){
 16         //opening file
 17         ifstream inFile(Filename, ios::in);
 18         if(inFile.bad()){
 19                 cout << "File error" << endl;
 20                 return ;
 21         }
 22          //get Teamlist for tournament
 23          while(!(inFile.eof())){
 24                  CTeam* temp = new CTeam;
 25                  temp->ParseTeam(inFile);
 26 
 27                  TeamList.push_back(temp);
 28          }
 29 }

在这里,我们将inFile传递给CTeam.ParseTeam,如下所示:

Here we pass inFile into CTeam.ParseTeam which looks like:

     30 void CTeam::ParseTeam(std::istream in){
     31   string readline;
     32   getline(in, readline);
     33   this->TeamName = readline;
     34   while(!(in.eof())&&(readline != " " || readline != "/n"))
     35   {
     36           getline(in, readline);
     37           this->Parse(readline);
     38   }
     39 }

我们得到了错误:

In file included from /usr/include/c++/4.4/ios:39,
                 from /usr/include/c++/4.4/ostream:40,
                 from /usr/include/c++/4.4/iostream:40,
                 from CTournament.h:11,
                 from CTournament.cpp:8:
/usr/include/c++/4.4/bits/ios_base.h: In copy constructor 'std::basic_ios<char, std::char_traits<char> >::basic_ios(const std::basic_ios<char, std::char_traits<char> >&)':
/usr/include/c++/4.4/bits/ios_base.h:790: error: 'std::ios_base::ios_base(const std::ios_base&)' is private
/usr/include/c++/4.4/iosfwd:47: error: within this context
/usr/include/c++/4.4/iosfwd: In copy constructor 'std::basic_istream<char, std::char_traits<char> >::basic_istream(const std::basic_istream<char, std::char_traits<char> >&)':
/usr/include/c++/4.4/iosfwd:53: note: synthesized method 'std::basic_ios<char, std::char_traits<char> >::basic_ios(const std::basic_ios<char, std::char_traits<char> >&)' first required here 
CTournament.cpp: In constructor 'CTournament::CTournament(const char*)':
CTournament.cpp:25: note: synthesized method 'std::basic_istream<char, std::char_traits<char> >::basic_istream(const std::basic_istream<char, std::char_traits<char> >&)' first required here 
CTournament.cpp:25: error:   initializing argument 1 of 'void CTeam::ParseTeam(std::istream)'

我知道在他不包括fstream的情况下,对此也有类似的问题.我们已经将其包含在两个头文件中.

I know there is a similar question about this where he did not include fstream. We have included it in both header files.

我认为可能是没有将正确的类型传递给PraseTeam的问题,但是我找不到关于如何将文件传递给ParseTeam来验证我是否正确执行操作的任何具体说明.

I thought maybe it was a problem of not passing the correct type into PraseTeam, but I could not find anything very specific on how else to pass the file into ParseTeam to verify whether I was doing it correctly or not.

谢谢.

推荐答案

您需要通过引用传递流,因为流通常不可复制:

You need to pass the stream by reference, because streams are not generally copyable:

void CTeam::ParseTeam(std::istream &in)

这篇关于将istream传递给函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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