c++ - 错误: no match for call to (std::string{aka std::basic_string<char>})(const char[5])'

标签 c++ file error-handling ofstream

因此,我在程序中编写了一个小小的if情况,在这种情况下,它将获取用户输入并使用该输入使用来自用户输入的信息来创建文本文件,然后使fstream对象稍后写入该文件。由于某种原因,我得到一个错误,说"no match for call to (std::string{aka std::basic_string<char>})(const char[5])'"另一个错误说"invalid user defined conversation from 'std::ofstream{aka std::basic_ofstream<char>}'to 'const char*'[-fpermissive]"现在我对这两个错误一无所知,所以我将非常感谢您的帮助,有关更多信息,这里是发生错误的代码段


if(choosingThis==1)
            {
                cout <<"Name your database"<< endl;
                
                string naming;
                cin >> naming;
                
                ofstream newie(naming); //first error took place here 

                ofstream newFile; 
                newFile.open(newie); //next error error took place here 
                
                cout <<"wanna insert some data?"<< endl;
                
                string yup;
                cin >> yup;
                
                bool probably;
                
                if(yup=="yes")
                {
                    probably=true;
                }
                else if(yup=="no")
                {
                    probably=false;
                }
                
                if(probably==true)
                {
                    cout <<"insert your data"<< endl;
                    
                    string dataforThenew;
                    cin >> dataforThenew;
                    
                    getline(cin,dataforThenew);
                    
                    fstream dataPasser;
                    dataPasser.open(newie);
                    
                    dataPasser << dataforThenew;
                    
                    cout <<"wanna go back?"<< endl;
                    
                    string yes;
                    cin >> yes;
                    
                    if(yes=="yes")
                    {
                        main();
                    }
                    else
                    {
                        cout <<"ok"<< endl;
                        
                        system("pause");
                        return 0;
                    }

最佳答案

看来您使用的是旧的编译器。从C++ 11开始,std::string可用于初始化std::ostream。根据编译器的不同,启用C++ 11的标志将为-std=c++11(gcc和clang)或/std:c++11(MSVC)。这应该解决第一个错误。
如果由于某种原因您陷入了不支持C++ 11的编译器中,则可以从std::string中提取C样式的字符串:

ofstream newie(naming.c_str());

第二个错误是因为您正在尝试做没有意义的事情。您想使用另一个fstream来对一个fstream进行open()。您不能将两个输出流传输到同一文件。您需要在这里解释您要尝试的操作,然后我们才能提出替代方案。
在此代码段中,您既没有使用newie也没有使用newFile,所以也许您只是想要fstream dataPasser(naming);

还有一件事:Undefined Behviour直接调用main(),不允许这样做。您需要找到另一种重新启动过程的方法(也许是循环?)。

关于c++ - 错误: no match for call to (std::string{aka std::basic_string<char>})(const char[5])',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63614480/

相关文章:

c++ - 为什么这个指针(WndProc 内的游戏)为 NULL?

c++ - VS2013 的奇怪行为

c - 用C中的空格分割每一行

reactjs - Apollo GraphQL 本地和全局错误处理

node.js - Express 中的嵌套错误处理

c++ - 排序后插入链表

c++ - 使用 clEnqueueWriteBuffer 的内存损坏 - OpenCL

file - 如何在一行 unix 命令中连接两个文件(删除第二个文件的第一行)?

python - 如何读取.bin 文件?

asp.net-core - 如何防止NLog处理引用库中的日志语句?