【问题标题】:Error: no match for call to (std::string{aka std::basic_string<char>})(const char[5])'错误:不匹配调用 (std::string{aka std::basic_string<char>})(const char[5])'
【发布时间】:2020-08-27 10:53:21
【问题描述】:

所以我在我的程序中编写了一个小的 if 情况,它获取用户输入并使用该输入使用来自用户输入的信息创建一个文本文件,然后创建一个 fstream 对象以稍后写入该文件。出于某种原因,我收到一个错误提示

"no match for call to (std::string{aka std::basic_string&lt;char&gt;})(const char[5])'"

还有一个错误提示

"invalid user defined conversation from 'std::ofstream{aka std::basic_ofstream&lt;char&gt;}'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;
                    }

【问题讨论】:

  • 在这两种情况下,您都将std::string 作为参数传递,但它应该是const char *。试试:newbie.c_str().
  • @vahancho 在第二种情况下,他使用 ofstream 打开一个 ofstream 而不是字符串。字符串是ofstream构造函数中的有效参数,所以我不知道他为什么会得到这个错误
  • @RoQuOTriX,啊,对了!

标签: c++ file error-handling ofstream


【解决方案1】:

看来您使用的是旧的编译器。自 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());

第二个错误是因为你试图做一些没有意义的事情。您想open() 使用另一个 fstream 的 fstream。您不能有两个输出流到同一个文件。您需要先说明您在此处尝试执行的操作,然后我们才能提出替代方案。
在这个 sn-p 中,您既没有使用newie 也没有使用newFile,所以也许您只是想要fstream dataPasser(naming);


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

【讨论】:

    猜你喜欢
    • 2017-03-10
    • 1970-01-01
    • 1970-01-01
    • 2015-04-28
    • 1970-01-01
    • 1970-01-01
    • 2014-07-13
    • 2021-10-28
    • 2021-09-16
    相关资源
    最近更新 更多