Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/149.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 调用'std::basic_of stream<;char,std::char_traits<;char>>;::基本流(std::string&;)和x27;_C++_String_Ofstream - Fatal编程技术网

C++ 调用'std::basic_of stream<;char,std::char_traits<;char>>;::基本流(std::string&;)和x27;

C++ 调用'std::basic_of stream<;char,std::char_traits<;char>>;::基本流(std::string&;)和x27;,c++,string,ofstream,C++,String,Ofstream,我试图编写一个程序,要求用户输入文件名,然后打开该文件。当我编译它时,我得到以下错误: no matching function for call to std::basic_ofstream<char, std::char_traits<char> >::basic_ofstream(std::string&) 调用std::basic_of stream::basic_of stream(std::string&)时没有匹配的函数 这是我的代码: usi

我试图编写一个程序,要求用户输入文件名,然后打开该文件。当我编译它时,我得到以下错误:

no matching function for call to std::basic_ofstream<char, 
std::char_traits<char> >::basic_ofstream(std::string&)
调用std::basic_of stream::basic_of stream(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;
int main()
{ 
弦乐;
cout>asegurado;
夹带流(asegurado,“”);
if(entrada.fail())
{
如果您使用的是C++11或更高版本,则只能使用
std::string
构造cout。通常使用
-std=C++11
(gcc,clang)来构造cout。如果您没有访问C++11的权限,则可以使用
C_str()
std::string的函数
const char*
传递给流的
构造函数

同样,您使用空字符串作为构造函数的第二个参数。如果提供,则第二个参数的类型必须为
ios\u base::openmode

有了这些,您的代码应该是

ofstream entrada(asegurado); // C++11 or higher


我还建议您阅读:

您的stream entrada(asegurado,“”)的构造函数
与的不匹配。第二个参数必须是a,请参见以下内容:

entrada ("example.bin", ios::out | ios::app | ios::binary);
                            //^ These are ios_base arguments for opening in a specific mode.
要使程序运行,只需从流的
构造函数中删除字符串文字:

ofstream entrada(asegurado);

如果您使用的是
c++03
或更低版本,则无法将
std::string
传递给流的
构造函数,您需要传递一个c字符串:

ofstream entrada(asegurado.c_str());

我不认为是这样。对于c++14,它仍然失败,问题是没有一个构造函数将字符串文本作为第二个参数。非常感谢!但是代码中的函数
c_str()
?我以前从未使用过它。@ben,这就是为什么我这样回答。我确实指出了打字错误以及您的归因。好的,回答了所有场景+1。我做到了,我只是将函数
c_str()
添加到了`ofstream entrada(asegurado);它工作正常,现在我将阅读为什么使用名称空间std;“被认为是不好的练习。非常感谢你所做的一切!格拉茨
ofstream entrada(asegurado.c_str());