C++:变量“std::ifstream ifs”有初始值设定项但类型不完整

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15080231/
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-27 19:00:21  来源:igfitidea点击:

C++: variable 'std::ifstream ifs' has initializer but incomplete type

c++fstreamreturn-type

提问by beakr

Sorry if this is pretty noobish, but I'm pretty new to C++. I'm trying to open a file and read it using ifstream:

对不起,如果这很菜鸟,但我对 C++ 还很陌生。我正在尝试打开一个文件并使用ifstream以下命令读取它:

vector<string> load_f(string file) {
  vector<string> text;

  ifstream ifs(file);
  string buffer, str_line;

  int brackets = 0;
  str_line = "";

  while ( getline(ifs, buffer) ) {
    buffer = Trim( buffer );
    size_t s = buffer.find_first_of("()");

    if (s == string::npos) str_line += "" + buffer;
    else {
      while ( s != string::npos ) {
        str_line += "" + buffer.substr(0, s + 1);
        brackets += (buffer[s] == '(' ? 1 : -1);

        if ( brackets == 0 ) {
          text.push_back( str_line );
          str_line = "";
        }

        buffer = buffer.substr(s + 1);
        s = buffer.find_first_of("()");
      }
    }
  }

  return text;
}

However, I'm getting the following error I'm not quite sure how to fix:

但是,我收到以下错误我不太确定如何解决:

variable 'std::ifstream ifs' has initializer but incomplete type

Answers very appreciated. Note that I never forgot to #include <fstream>, since many have gotten the error due to just forgetting to include the header.

答案非常感谢。请注意,我从未忘记过#include <fstream>,因为许多人因为忘记包含标题而得到错误。

EDIT:

编辑:

Turns out that I actually did forget to include fstream, but I had forgotten due to moving the function to another file.

事实证明,我确实忘记了 include fstream,但由于将函数移动到另一个文件而忘记了。

回答by mksteve

This seems to be answered - #include <fstream>.

这似乎得到了回答 - #include <fstream>

The message means :-

该消息意味着:-

incomplete type- the class has not been defined with a full class. The compiler has seen statements such as class ifstream;which allow it to understand that a class exists, but does not know how much memory the class takes up.

incomplete type- 该类尚未定义为完整的类。编译器已经看到诸如class ifstream;允许它理解一个类存在的语句,但不知道该类占用了多少内存。

The forward declaration allows the compiler to make more sense of :-

前向声明允许编译器更有意义:-

void BindInput( ifstream & inputChannel ); 

It understands the class exists, and can send pointers and references through code without being able to create the class, see any data within the class, or call any methods of the class.

它了解类的存在,并且可以通过代码发送指针和引用,而无需创建类、查看类中的任何数据或调用类的任何方法。

The has initializerseems a bit extraneous, but is saying that the incomplete object is being created.

has initializer似乎有点多余,但称正在创建完全的对象。