No matching function for ifstream open()
The issue arises in the code snippet:
std::ifstream file; file.open(name); // the error is here
Dev C encounters errors "no matching function for call 'std::basic_ifstream
Solution:
To resolve this issue, convert the std::string to a C-style string using the c_str() member function:
file.open(name.c_str());
Alternatively, you can directly initialize the ifstream object with the C-style string:
std::ifstream file(name.c_str());
In addition, consider declaring loadNumbersFromFile() as follows:
std::vector<int> loadNumbersFromFile(const std::string& name)
This change indicates that the function does not modify its argument and prevents an unnecessary copy.
The above is the detailed content of Why Doesn't `ifstream::open()` Work with `std::string` Arguments?. For more information, please follow other related articles on the PHP Chinese website!