188

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.

6
  • 7
    This answer helped me. In my case, it was because I removed another header file that included fstream. Solution was to include fstream. May 25, 2014 at 16:15
  • 14
    And don't confuse with <iostream>. Only <fstream> will do.
    – Joachim W
    Sep 4, 2014 at 20:59
  • 11
    This question is NOT too localized. This explained exactly the problem I was having.
    – Syndog
    Dec 5, 2014 at 0:05
  • 4
    +1 for OP including the answer. Helped me when merging another dev's code that was built with stale project includes not sent to me for the merge. Clearly not too localized or narrow.
    – Kenigmatic
    Jun 4, 2015 at 17:52
  • 3
    Possible duplicate of Infile incomplete type error
    – Rick Smith
    Apr 8, 2016 at 18:53

1 Answer 1

161

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.

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.