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

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

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.

EDIT:

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

This question is related to c++ fstream return-type

The answer is


This seems to be answered - #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.

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.


Examples related to c++

Method Call Chaining; returning a pointer vs a reference? How can I tell if an algorithm is efficient? Difference between opening a file in binary vs text How can compare-and-swap be used for a wait-free mutual exclusion for any shared data structure? Install Qt on Ubuntu #include errors detected in vscode Cannot open include file: 'stdio.h' - Visual Studio Community 2017 - C++ Error How to fix the error "Windows SDK version 8.1" was not found? Visual Studio 2017 errors on standard headers How do I check if a Key is pressed on C++

Examples related to fstream

clear data inside text file in c++ fstream won't create a file C++: variable 'std::ifstream ifs' has initializer but incomplete type How to read line by line or a whole text file at once? Using C++ filestreams (fstream), how can you determine the size of a file? Reading from text file until EOF repeats last line

Examples related to return-type

How to specify multiple return types using type-hints Return from lambda forEach() in java Return different type of data from a method in java? Apply pandas function to column to create multiple new columns? C++: variable 'std::ifstream ifs' has initializer but incomplete type Returning anonymous type in C# How do I make the return type of a method generic? How to return result of a SELECT inside a function in PostgreSQL? Can two Java methods have same name with different return types? What should main() return in C and C++?