c++ - No matching function - ifstream open() -
this part of code error:
std::vector<int> loadnumbersfromfile(std::string name) { std::vector<int> numbers; std::ifstream file; file.open(name); // error here if(!file) { std::cout << "\nerror\n\n"; exit(exit_failure); } int current; while(file >> current) { numbers.push_back(current); file.ignore(std::numeric_limits<std::streamsize>::max(), '\n'); } return numbers; }
and well, kind of have no idea going on. whole thing compiles in vs. need compile dev cpp.
i commented out line throwing errors in code above. errors are:
no matching function call 'std::basic_ifstream::open(std::string&)
no matching function call 'std::basic_ofstream::open(std::string&)
in different parts of code errors 'numeric_limits not member of std', or 'max() has not been declared', although exist in iostream class , works in vs.
why getting error?
change to:
file.open(name.c_str());
or use constructor there no reason separate construction , open:
std::ifstream file(name.c_str());
support std::string
argument added in c++11.
as loadnumbersfromfile()
not modify argument pass std::string const&
document fact , avoid unnecessary copy.
Comments
Post a Comment