C++ 传递对 std::ifstream 的引用作为参数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27501160/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 20:48:12  来源:igfitidea点击:

Pass a reference to std::ifstream as parameter

c++functionifstream

提问by Newbie

I'm trying to write a function with an ifstream&argument.

我正在尝试编写一个带ifstream&参数的函数。

void word_transform(ifstream & infile)
{
    infile("content.txt");
    //etc
}

which gave me an error:

这给了我一个错误:

Type 'ifstream' (aka 'basic_ifstream ') does not provide a call operator.

类型“ifstream”(又名“basic_ifstream”)不提供调用运算符。

Can you please me what's wrong?

你能帮我看看有什么问题吗?

回答by jpo38

call operatoris a function like operator()( params )allowing to use the syntax myObject( params ).

调用运算符是一个函数,类似于operator()( params )允许使用语法myObject( params )

So, when you write infile(...), you are trying to us a call operator.

因此,当您编写 时infile(...),您是在尝试向我们呼叫接线员。

What you are trying to do is to open a file, use the openmethod:

您要做的是打开一个文件,使用以下open方法:

void word_transform(ifstream & infile)
{
    infile.open("content.txt",std::ios_base::in);
    if ( infile.is_open() )
        infile << "hello";
    infile.close();
}

But, as commented, it does not really make sense to pass infile reference to such a function. You may consider:

但是,正如所评论的,将 infile 引用传递给这样的函数并没有真正意义。你可以考虑:

void word_transform(istream& infile)
{
    infile << "hello";
}

int main()
{
    ifstream infile;
    infile.open("content.txt",std::ios_base::in);
    if ( infile.is_open() )
        word_transform( infile );
    infile.close();
    return 0;
}

Or:

或者:

void word_transform()
{
    ifstream infile;
    infile.open("content.txt",std::ios_base::in);
    if ( infile.is_open() )
        infile << "hello";
    infile.close();
}

int main()
{
    word_transform();
    return 0;
}

回答by nvoigt

You attempt to call operator()on your parameter. That will not work. Are you trying to open a file? If you get an ifstreamas parameter, it should be open from the start because you opened it outside your function. Passing a stream and then opening it inside your function does not make sense.

您尝试调用operator()您的参数。那不管用。你想打开一个文件吗?如果你得到一个ifstreamas 参数,它应该从一开始就打开,因为你在函数之外打开了它。传递一个流然后在你的函数中打开它是没有意义的。

void word_transform(std::ifstream& infile)
{
    // read something from infile
}

int main()
{
   std::ifstream file("content.txt");

   // error checks

   word_transform(file);

   return 0;
}

回答by Codor

To my understanding, the call operator is operator(), which apparently is not defined for ifstream. You have to do something different with the argument of word_transform.

据我了解,调用运算符是operator(),显然没有为ifstream. 你必须对 的参数做一些不同的事情word_transform

回答by ravi

infile("content.txt");

Note that this would try to call operator() on already created object of type infile. As no such operator exists from ifstream , you got an error.

请注意,这将尝试在已创建的 infile 类型对象上调用 operator()。由于 ifstream 中不存在这样的运算符,因此出现错误。

Rather you should do:-

相反,您应该这样做:-

infile.open("content.txt");

回答by Walter

Typically, (references to) streams are passed to functions for I/O. This means the function should take std::istreamor std::ostreamargument, but not std::ifstreamor std::ofstream. This way your function can be used with any stream object derived from std::istream, including std::cin, std::istrstream, and std::ifstream.

通常,(对)流的引用被传递给 I/O 函数。这意味着函数应该采用 std::istreamorstd::ostream参数,而不是std::ifstreamor std::ofstream。这样,你的函数可以用任何流对象使用源自std::istream包括std::cinstd::istrstream,和std::ifstream

As nvoigt said, passing an std::ifstreamto a function for opening makes little sense. It is definitely not clear to the caller that its stream may be closed and opened to another file.

正如 nvoigt 所说,将 an 传递std::ifstream给打开的函数毫无意义。调用者肯定不清楚它的流可能被关闭并打开到另一个文件。