1

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;
19
  • 1
    Cannot reproduce with c++11; live link: godbolt.org/g/1kpcsJ Commented Aug 11, 2018 at 13:58
  • Can't reproduce. What's your compiler and OS, and what command are you using to compile it? Commented Aug 11, 2018 at 13:58
  • 11
    @HansPassant — standard library headers are supposed to be self-contained. They can be included in any order. Commented Aug 11, 2018 at 14:02
  • 1
    Sounds like you're including a bad <string> header. Commented Aug 11, 2018 at 14:03
  • 1
    Is there a precompiled header, such as stdafx.h in your code somewhere? Commented Aug 11, 2018 at 14:06

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

Comments

1

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.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.