如何解决错误:没有匹配调用std :: string {aka std :: basic_string <char>}const char [5]'
因此在我的程序中对小情况进行了编码,在这种情况下,它获得用户输入并使用该输入使用来自用户输入的信息来创建文本文件,然后使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());
第二个错误是因为您正在尝试做没有意义的事情。您想open()
使用另一个fstream。您不能将两个输出流传输到同一文件。您需要在这里解释一下自己想做什么,然后我们才能提出替代方案。
在此代码段中,您既没有使用newie
也没有使用newFile
,所以也许您只是想使用fstream dataPasser(naming);
?
另一件事:未定义行为直接调用main()
,您不能这样做。您需要找到另一种重新启动过程的方法(也许是循环?)。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。