【发布时间】:2020-08-27 10:53:21
【问题描述】:
所以我在我的程序中编写了一个小的 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;
}
【问题讨论】:
-
在这两种情况下,您都将
std::string作为参数传递,但它应该是const char *。试试:newbie.c_str(). -
@vahancho 在第二种情况下,他使用 ofstream 打开一个 ofstream 而不是字符串。字符串是ofstream构造函数中的有效参数,所以我不知道他为什么会得到这个错误
-
@RoQuOTriX,啊,对了!
标签: c++ file error-handling ofstream