简体   繁体   中英

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. 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. Depending on compiler, the flag to enable C++11 would be -std=c++11 (gcc and clang) or /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 :

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. 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); ?


One more thing: calling main() directly is Undefined Behviour, you are not allowed to do that. You need to find another way to restart your procedure (a loop perhaps?).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM