Why do I get a “no matching function” error when I open an fstream with the file name in a std::string?

后端 未结 1 553
慢半拍i
慢半拍i 2021-01-22 14:37

I am trying to write a program that reads in a list of strings from a file and checks what strings are missing from a second file and prints them to the screen. However, I am cu

相关标签:
1条回答
  • 2021-01-22 15:39

    This construct works for standard C++ 11 and newer compilers:

    std::string filename;
    //...
    IN_FILE.open(filename);
    

    This is basically the code you have now. However, the above will not work with pre-standard C++ 11 compilers (C++ 98, 03).

    If you are compiling using a pre-standard C++ 11 compiler, then the code above should be:

    std::string filename;
    //...
    IN_FILE.open(filename.c_str()); // Note the null-terminated const char *
    

    Basically, the std::string version of open did not exist before C++ 11. Before C++ 11, you had to specify the name using a C-style, null-terminated string.

    Thus the error you are seeing is because you are compiling in a version of C++ before C++ 11.

    0 讨论(0)
提交回复
热议问题