C ++:variable'std :: ifstream ifs'具有初始化器但不完整类型 [英] C++: variable 'std::ifstream ifs' has initializer but incomplete type

查看:4090
本文介绍了C ++:variable'std :: ifstream ifs'具有初始化器但不完整类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对不起,如果这是非常noobish,但我是新到C ++。我尝试使用 ifstream 打开一个文件并读取它:

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:

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

我从来没有忘记 #include< fstream> ,因为许多人因为刚刚忘记包括头而得到错误。

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.

编辑:

结果证明我确实忘记了 fstream

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

推荐答案

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

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

此讯息表示: -

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

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.

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

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 initializer seems a bit extraneous, but is saying that the incomplete object is being created.

这篇关于C ++:variable'std :: ifstream ifs'具有初始化器但不完整类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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