我正在学习fstream类,但遇到了一些麻烦。我创建了几个txt文件,一个是笑话,另一个是妙语连珠(joke.txt)和(punchline.txt),只是为了读取和显示内容。我要求用户输入文件名,如果找到它应该打开它,清除标志然后读取内容。但是我无法测试它读取的内容,因为我目前正在遇到有关已删除函数的错误,但我不知道这意味着什么。
错误1:
"IntelliSense: function "std::basic_ifstream<_Elem, _Traits>::basic_ifstream(const std::basic_ifstream<_Elem, _Traits>::_Myt &) [with _Elem=char, _Traits=std::char_traits<char>]" (declared at line 818 of "C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\include\fstream") cannot be referenced -- it is a deleted function
第二个错误与第一个错误完全相同,但是针对第二个函数(displayLastLine())
还有第三个错误:
Error 1 error C2280: 'std::basic_ifstream<char,std::char_traits<char>>::basic_ifstream(const std::basic_ifstream<char,std::char_traits<char>> &)' : attempting to reference a deleted function
这是我的代码:
#include "stdafx.h"
#include <string>
#include <iostream>
#include <fstream>
using namespace std;
void displayAllLines(ifstream joke);
void displayLastLine(ifstream punchline);
int main()
{
string fileName1, fileName2;
ifstream jokeFile, punchlineFile;
// Desribe the assigned project to the User
cout << "This program will print a joke and its punch line.\n\n";
cout << "Enter the name of the joke file (ex. joke.txt): ";
cin >> fileName1;
jokeFile.open(fileName1.data());
if (!jokeFile)
{
cout << " The file " << fileName1 << " could not be opened." << endl;
}
else
{
cout << "Enter name of punch line file (ex. punchline.txt): ";
cin >> fileName2;
punchlineFile.open(fileName2.data());
if (!punchlineFile)
{
cout << " The file " << fileName2 << " could not be opened." << endl;
jokeFile.close();
}
else
{
cout << endl << endl;
displayAllLines(jokeFile);
displayLastLine(punchlineFile);
cout << endl;
jokeFile.close();
punchlineFile.close();
}
}
// This prevents the Console Window from closing during debug mode
cin.ignore(cin.rdbuf()->in_avail());
cout << "\nPress only the 'Enter' key to exit program: ";
cin.get();
return 0;
}
void displayAllLines(ifstream joke)
{
joke.clear();
joke.seekg(0L, ios::beg);
string jokeString;
getline(joke, jokeString);
while (!joke.fail())
{
cout << jokeString << endl;
}
}
void displayLastLine(ifstream punchline)
{
punchline.clear();
punchline.seekg(0L, ios::end);
string punchString;
getline(punchline, punchString);
while (!punchline.fail())
{
cout << punchString << endl;
}
}