我正在尝试编写一个程序,要求用户输入文件名,然后打开该文件。当我编译它时,我收到以下错误:
no matching function for call to std::basic_ofstream<char,
std::char_traits<char> >::basic_ofstream(std::string&)
这是我的代码:
using namespace std;
int main()
{
string asegurado;
cout << "Nombre a agregar: ";
cin >> asegurado;
ofstream entrada(asegurado,"");
if (entrada.fail())
{
cout << "El archivo no se creo correctamente" << endl;
}
}
std::ofstream http://en.cppreference.com/w/cpp/io/basic_ofstream只能用一个构造std::string
如果您有 C++11 或更高版本。通常是这样完成的-std=c++11
(海湾合作委员会,铿锵声)。如果您无法访问 c++11 那么您可以使用c_str()
的函数std::string
通过一个const char *
to the ofstream
构造函数。
Also as Ben https://stackoverflow.com/users/1294207/ben has 指出 https://stackoverflow.com/a/35045920/4342498您使用空字符串作为构造函数的第二个参数。如果提供第二个参数,则其类型必须为ios_base::openmode
.
有了这一切,你的代码应该是
ofstream entrada(asegurado); // C++11 or higher
or
ofstream entrada(asegurado.c_str()); // C++03 or below
我还建议您阅读:为什么是“using namespace std;”被认为是不好的做法? https://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)