尝试引用已删除的函数

22
我正在学习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;
     }
 }

2
你已经采取了哪些调试措施来解决问题?你尝试过编写一个最小化的工作示例吗? - Sebastian Schmitz
你调用了一个标准不支持的函数。 - Theolodis
可能是 [error: use of deleted function] 的重复问题。请参考此链接:https://dev59.com/j2025IYBdhLWcg3wf2OE - Theolodis
1个回答

41

你调用了一个被删除的函数,即类 std::ifstream 的拷贝构造函数。

如果您查看参考文献,您会注意到,拷贝构造函数是不允许的。

因此,您应该使用以下语句:

void displayAllLines(ifstream joke);
void displayLastLine(ifstream punchline);

你应该使用按引用传递的函数调用:

void displayAllLines(ifstream& joke);
void displayLastLine(ifstream& punchline);

使用引用的行为就像使用副本调用方法一样,但实际上您是在操作原始对象而不是新构造的副本对象。请记住这一点,以便在进一步使用按引用调用时加以注意。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接