[英]error: no match for call to '(const std::basic_string<char>) ()'
[英]Error: no match for call to (std::string{aka std::basic_string<char>})(const char[5])'
所以我在我的程序中編寫了一個小的 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;
}
看來您使用的是舊編譯器。 從 C++11 開始, std::string
可用於初始化std::ostream
。 根據編譯器的不同,啟用 C++11 的標志將是-std=c++11
(gcc 和 clang)或/std:c++11
(MSVC)。 這應該修復第一個錯誤。
如果由於某種原因你被不支持 C++11 的編譯器卡住了,你可以從std::string
提取 C 風格的std::string
:
ofstream newie(naming.c_str());
第二個錯誤是因為你試圖做一些沒有意義的事情。 您想使用另一個 fstream 來open()
一個 fstream。 同一個文件不能有兩個輸出流。 在我們提出替代方案之前,您需要在此處說明您嘗試執行的操作。
在這個片段中,你既沒有使用newie
也沒有使用newFile
,所以也許你只是想要fstream dataPasser(naming);
?
還有一件事:直接調用main()
是 Undefined Behviour,你不能這樣做。 您需要找到另一種方法來重新啟動您的程序(也許是循環?)。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.