Soifam
Soifam

Reputation: 51

Including <string>, Use of incomplete type 'std::string'

In this example code, <fstream> works fine but <string> doesn't: (errors in comments)

#include <iostream>
#include <fstream>
#include <string>

int main(){
    using namespace std;
    string line; //incomplete type is not allowed
    ifstream ifile("test.in");
    if(ifile.is_open()){
        while(ifile.good()){
            getline(ifile,line); //identifier "getline" is undefined
        }
    }
}

The error of getline() becomes "namespace "std" has no member "getline"" when I change the code to:

int main(){
    std::string line; //incomplete type is not allowed
    std::ifstream ifile("test.in");
    if(ifile.is_open()){
        while(ifile.good()){
            std::getline(ifile,line); //namespace "std" has no member "getline"
        }
    }
}

This code "fixes" the error of the declaration of string line:

std::string* line;

or also:

using namespace std;
string* line;

Upvotes: 1

Views: 3256

Answers (2)

Soifam
Soifam

Reputation: 51

Thanks you all for your help. I solved the problem, but I still doesn't know what was its cause. I don't know if it was the configuration of the compiler (probably not because other headers were working fine). I don't think the compiler was broken, beacuse after reinstalling it the problem persisted.

The issue was magically solved after reinstalling the whole Visual Studio Code and completley reconfiguring the compilerand build options.

Upvotes: 1

Michael Veksler
Michael Veksler

Reputation: 8475

I had a similar thing twice. It is very easy to accidentally create an empty "vector", "set", "map", etc files in your project, especially if you accidentally paste source code to your terminal (luckily we include <string> and not >string< ).

Visual c++ will gladly use the empty header instead of the standard one, without giving a single warning. Scan your solution directory for a file named vector.

If that does not help, visual studio has a debugging mode that shows all included files.

If this is gcc or clang, you can run the compiler with -E flag, instead of -c. This will create a text file with all included text in one file (or on stdout). This will give you a hint what is going on. This is how I found my empty "set" file.

Upvotes: 1

Related Questions