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;
MivVG
  • 679
  • 4
  • 16
Soifam
  • 51
  • 4
  • 1
    Cannot reproduce with c++11; live link: https://godbolt.org/g/1kpcsJ – Richard Critten Aug 11 '18 at 13:58
  • Can't reproduce. What's your compiler and OS, and what command are you using to compile it? – Silvio Mayolo Aug 11 '18 at 13:58
  • My crystal ball says that you should put the #include for string *first*. Headers like iostream also need to know about std::string and that can get awkward in a hurry. – Hans Passant Aug 11 '18 at 14:00
  • 11
    @HansPassant — standard library headers are supposed to be self-contained. They can be included in any order. – Pete Becker Aug 11 '18 at 14:02
  • 1
    Sounds like you're including a bad `` header. – ShadowRanger Aug 11 '18 at 14:03
  • 1
    Is there a precompiled header, such as `stdafx.h` in your code somewhere? – user7860670 Aug 11 '18 at 14:06
  • @SilvioMayolo Windows 10 64bit, minwg64, g++ -o res main.cpp -luuid -std=c++11 – Soifam Aug 11 '18 at 14:08
  • @VTT no, anywhere – Soifam Aug 11 '18 at 14:09
  • @ShadowRanger and what's the solution? – Soifam Aug 11 '18 at 14:10
  • Look in the directory that has `` and do a visual check of the date modified times - they should all be the same. – Richard Critten Aug 11 '18 at 14:24
  • @RichardCritten Seems alright, like this? https://gyazo.com/3109cc8cd345cd96aac0420ec28b02b1 – Soifam Aug 11 '18 at 14:31
  • @PeteBecker • Except for ``, that one is not idempotent. (I think that is the only Standard C++ header that is not idempotent.) – Eljay Aug 11 '18 at 14:36
  • The only reason I can think is that `` isn't exist. – con ko Aug 11 '18 at 16:06
  • If rearranging the #includes produces the exact same error then it is very likely that the string header file is corrupted. Look at it with a text editor, you'd expect it to be completely empty. This kind of file damage is quite unhealthy, be sure to at least run chkdsk.exe to verify the drive. Then reinstall mingw to recover. – Hans Passant Aug 11 '18 at 16:09
  • @HansPassant The string file is not empty but it has some descriptions and licensing disclaimers in it. I don't think it's corrupted – Soifam Aug 11 '18 at 16:30
  • See [why `eof()` in a loop condition is wrong](https://stackoverflow.com/q/5605125/9254539). `while(ifile.good())` is bad for the same reason. – eesiraed Aug 11 '18 at 17:22
  • @Soifam: The solution is to fix the `` header, likely by reinstalling your compiler (or figure out what erroneous `` header is taking its place). I can't give a better answer, because I can't psychically determine all possible things wrong with your system. – ShadowRanger Aug 11 '18 at 17:49
  • Your compiler installation is broken, remove and reinstall. – n. m. could be an AI Aug 11 '18 at 19:52
  • @Eljay — `` involves a different consideration. It can be included multiple times, with different effects depending on context (specifically, whether `NDEBUG` is defined). That’s different from the rest of the standard headers, which can be included multiple times but always mean the same thing. The issue here, though, isn’t multiple inclusions, but order of includes, and the standard headers are all supposed to be immune to include order. That includes ``, because standard headers aren’t supposed to be affected by it. – Pete Becker Aug 12 '18 at 12:46

2 Answers2

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.

Michael Veksler
  • 8,217
  • 1
  • 20
  • 33
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.

Soifam
  • 51
  • 4