Search code examples
c++inputioheader-files

"Incomplete type"-error when trying to open ifstream


I am trying to read a file in C++, using the built in compiler of Visual Studio 2019. Following some examples I found, I try this:

#include <iostream>
using namespace std;

int main() {
    ifstream file("test.txt");
    string input = "";
    while (file >> input) {
        std::cout << input;
    }
    std::cout << "Hello World!\n";
}

However, while compiling Visual Studio shows the error incomplete type is not allowed. The output window shows error C2079: 'file' uses undefined class 'std::basic_ifstream<char,std::char_traits<char>>'.

Following this thread like so:

ifstream file;
file.open("test.txt");

gives me the same error.

  1. What does the error mean?
  2. How do I fix it?

Solution

  • You need to include additionally two headers

    #include <fstream>
    #include <string>
    

    Pay attention to that instead of this declaration

    string input = "";
    

    you could just write

    string input;