试图引用已删除的功能

jac*_*nSD 20 c++ string fstream function ifstream

我正在尝试了解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
Run Code Online (Sandbox Code Playgroud)

第二个错误是完全相同但是对于第二个函数(displayLastLine())

和错误3:

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
Run Code Online (Sandbox Code Playgroud)

这是我的代码:

#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;
     }
 }
Run Code Online (Sandbox Code Playgroud)

The*_*dis 39

你确实调用了一个已删除的函数,它是该类的复制构造函数std::ifstream.

如果您看一下您注意到的引用,则不允许使用复制构造函数.

所以不要使用:

void displayAllLines(ifstream joke);
void displayLastLine(ifstream punchline);
Run Code Online (Sandbox Code Playgroud)

你应该通过引用来处理调用:

void displayAllLines(ifstream& joke);
void displayLastLine(ifstream& punchline);
Run Code Online (Sandbox Code Playgroud)

使用引用的行为就像使用副本调用方法一样,但实际上您正在操作原始对象而不是新的复制构造对象.请记住,以便通过引用进一步使用该呼叫.