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

Popular posts from this blog

php - Wordpress website dashboard page or post editor content is not showing but front end data is showing properly -

How to get the ip address of VM and use it to configure SSH connection dynamically in Ansible -

javascript - Get parameter of GET request -