简体   繁体   English

错误:不匹配调用 (std::string{aka std::basic_string<char> })(const char[5])&#39;

[英]Error: no match for call to (std::string{aka std::basic_string<char>})(const char[5])'

So im coding a small if situation in my program where it gets user input and uses that input to make a text file using the info from the user input, then makes an fstream object to write to that file later.所以我在我的程序中编写了一个小的 if 情况,它获取用户输入并使用该输入使用来自用户输入的信息制作一个文本文件,然后制作一个 fstream 对象以稍后写入该文件。 and for some reason i get an error that says出于某种原因,我收到一个错误消息,说

"no match for call to (std::string{aka std::basic_string<char>})(const char[5])'"

with another error saying另一个错误说

"invalid user defined conversation from 'std::ofstream{aka std::basic_ofstream<char>}'to 'const char*'[-fpermissive]"

now i don't really understand anything about these 2 errors so i would really appreciate some help, for more info here is the chunk of code the errors took place in现在我对这两个错误一无所知,所以我真的很感激一些帮助,有关更多信息,这里是发生错误的代码块


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;
                    }

It seems that you are using an old compiler.看来您使用的是旧编译器。 std::string can be used to initialize std::ostream since C++11.从 C++11 开始, std::string可用于初始化std::ostream Depending on compiler, the flag to enable C++11 would be -std=c++11 (gcc and clang) or /std:c++11 (MSVC).根据编译器的不同,启用 C++11 的标志将是-std=c++11 (gcc 和 clang)或/std:c++11 (MSVC)。 This should fix first error.这应该修复第一个错误。
If for some reason you are stuck with compiler that doesn't support C++11, you can extract C-style string from std::string :如果由于某种原因你被不支持 C++11 的编译器卡住了,你可以从std::string提取 C 风格的std::string

ofstream newie(naming.c_str());

Second error is because you are trying to do something that doesn't make sense.第二个错误是因为你试图做一些没有意义的事情。 You want to open() an fstream using another fstream.您想使用另一个 fstream 来open()一个 fstream。 You cannot have two output streams to the same file.同一个文件不能有两个输出流。 You need to explain what you trying to do here before we can suggest alternatives.在我们提出替代方案之前,您需要在此处说明您尝试执行的操作。
In this snippet, you are not using either newie nor newFile , so perhaps you simply wanted fstream dataPasser(naming);在这个片段中,你既没有使用newie也没有使用newFile ,所以也许你只是想要fstream dataPasser(naming); ? ?


One more thing: calling main() directly is Undefined Behviour, you are not allowed to do that.还有一件事:直接调用main()是 Undefined Behviour,你不能这样做。 You need to find another way to restart your procedure (a loop perhaps?).您需要找到另一种方法来重新启动您的程序(也许是循环?)。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 错误:无法匹配'(const std :: basic_string <char>)()' - error: no match for call to '(const std::basic_string<char>) ()' C ++错误:无法调用&#39;(std :: string {aka std :: basic_string <char> })(std :: string&)&#39; - C++ Error: no match for call to ‘(std::string {aka std::basic_string<char>}) (std::string&)’ 错误:与“ operator *”不匹配(操作数类型为“ std::string {aka std basic_string <char> }&#39;和{aka std basic_string <char> }&#39;) - error: no match for 'operator*' (operand types are 'std: :string {aka std basic_string<char>}' and {aka std basic_string<char>}') 错误:无法从&#39;std :: string * {aka std :: basic_string转换 <char> *}&#39;为&#39;std :: string {aka std :: basic_string <char> }&#39;| - error: could not convert from 'std::string* {aka std::basic_string<char>*}' to 'std::string {aka std::basic_string<char>}'| 'operator*' 不匹配(操作数类型为 'const string' {aka 'const std::__cxx11::basic_string<char> '})</char> - no match for 'operator*' (operand type is 'const string' {aka 'const std::__cxx11::basic_string<char>'}) 错误:传递&#39;const string {aka const std::basic_string<char> }&#39; 作为 ... 的 &#39;this&#39; 参数丢弃限定符 [-fpermissive] - error: passing ‘const string {aka const std::basic_string<char>}’ as ‘this’ argument of ...discards qualifiers [-fpermissive] 错误:传递&#39;const字符串{aka const std :: __ cxx11 :: basic_string <char> }&#39;作为&#39;this&#39;参数 - Error: passing ‘const string {aka const std::__cxx11::basic_string<char>}’ as ‘this’ argument std :: string {aka std :: basic_string <char> 分配给&#39;&#39;&#39;char *&#39; - std::string {aka std::basic_string<char>}' to 'char*' in assignment| 错误:“ operator ==”不匹配(操作数类型为“ Seat”和“ std :: string {aka std :: basic_string <char> }&#39;) - error: no match for 'operator==' (operand types are 'Seat' and 'std::string {aka std::basic_string<char>}') 错误:无法转换&#39;std :: string {aka std :: basic_string <char> 初始化时&#39;&#39;到&#39;char *&#39; - error: cannot convert ‘std::string {aka std::basic_string<char>}’ to ‘char*’ in initialization
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM