c++ - no matching function for call to `std::basic_ofstream<char, std::char_traits<char> >::basic_ofstream(std::string&)' -
i trying write program ask user file name , opens file. when compile following error:
no matching function call std::basic_ofstream<char, std::char_traits<char> >::basic_ofstream(std::string&) this code:
using namespace std; int main() { string asegurado; cout << "nombre agregar: "; cin >> asegurado; ofstream entrada(asegurado,""); if (entrada.fail()) { cout << "el archivo no se creo correctamente" << endl; } }
std::ofstream can constructed std::string if have c++11 or higher. typically done -std=c++11 (gcc, clang). if not have access c++11 can use c_str() function of std::string pass const char * ofstream constructor.
also ben has pointed out using empty string second parameter constructor. second parameter if proivided needs of type ios_base::openmode.
with code should be
ofstream entrada(asegurado); // c++11 or higher or
ofstream entrada(asegurado.c_str()); // c++03 or below i suggest read: why “using namespace std;” considered bad practice?
Comments
Post a Comment