No matching function - ifstream open()
Change to:
file.open(name.c_str());
or just use the constructor as there is no reason to separate construction and open:
std::ifstream file(name.c_str());
Support for std::string
argument was added in c++11.
As loadNumbersFromFile()
does not modify its argument pass by std::string const&
to document that fact and avoid unnecessary copy.
how to fix no matching function ifstream error?
According to std::basic_ifstream, the constructor does not take string&
until C++11. Prior to C++11, it only takes const char *
. Easiest solution to your problem is:
ifstream infile(file.c_str());
std::string::c_str()
gets the underlying char pointer of the string so you can use in the constructor. Or you can use the C++11 as suggested in comments but it depends on your compiler version (looks like your compiler does not support it).
Why do I get a no matching function error when I open an fstream with the file name in a std::string?
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.
No matching call function error for fstream
In your code, fname
, dname
, and ename
are variables of type std::string
, by default. However, the fstream::open
requires a const char *
value passed to it for a filename.
Here is how the function should be used:
void open (const char* filename,
ios_base::openmode mode = ios_base::in | ios_base::out);
Obtained from C++ Reference: std::fstream::open
To do this, add the .c_str()
function to the end of the filename variable:
istream.open(fname.c_str());
nstream.open(ename.c_str());
istream.open(dname.c_str());
This will remove that error.
no matching function for call to ‘std::basic_ifstream<char>::basic_ifstream(QString&)’
Use QFile instead of std::ifstream.
My initial reasoning for preferring QFile over std::ifstream was because in general if I'm writing code that is using QString (or another Qt framework class), and there are other Qt framework classes that achieve what I'm trying to do (in this case QFile), I'm going to prefer that simply because it's going to be easier and I don't have to worry about converting types/weird edge cases.
Scheff added a great reason to prefer QFile when using QStrings that I totally overlooked. He said:
std::string is actually encoding agnostic where QString provides methods to convert to and from various encodings, and the internal handling of strings in Qt is done with a well-defined encoding. Hence, the usage of QFile will prepare the application for better handling of encoding and locale stuff than "the hack" with .toStdString().c_str().
C++ ifstream error using string as opening file path.
Change
ifstream file(filename);
to
ifstream file(filename.c_str());
Because the constructor for an ifstream
takes a const char*
, not a string
pre-C++11.
Related Topics
Why Can't I Use Float Value as a Template Parameter
How Does Generic Lambda Work in C++14
C/C++: Force Bit Field Order and Alignment
How to Validate User Input as a Double in C++
How to Change Mode from C++98 Mode in Dev-C++ to a Mode That Supports C++0X (Range Based For)
#Include All .Cpp Files into a Single Compilation Unit
Best C++ Ide or Editor For Windows
What Requirements Must Std::Map Key Classes Meet to Be Valid Keys
How to Make a .Lib File When Have a .Dll File and a Header File
Do Rvalue References to Const Have Any Use
Create N-Element Constexpr Array in C++11
How to Create Directory Tree in C++/Linux
How Is a Variable At the Same Address Producing 2 Different Values