Jan Berndt
Jan Berndt

Reputation: 1055

"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?

Upvotes: 2

Views: 1455

Answers (1)

Vlad from Moscow
Vlad from Moscow

Reputation: 311078

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;

Upvotes: 4

Related Questions