试图引用已删除的功能

时间:2014-05-12 07:55:27

标签: c++ string function fstream 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

第二个错误完全相同,但是对于第二个函数(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

这是我的代码:

#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 个答案:

答案 0 :(得分:38)

您确实调用了已删除的函数,它是类std::ifstream的复制构造函数。

如果您看一下at the reference,请注意,不允许复制构造函数。

所以不要使用:

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

你应该通过引用来处理电话:

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

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

答案 1 :(得分:0)

关于以前通过引用而不是值传递的答案,请记住,对已定义函数中变量的任何更改也会影响原始声明的变量(在main()中)。