【问题标题】:attempting to reference a deleted function试图引用已删除的函数
【发布时间】:2014-06-29 12:04:16
【问题描述】:

我正在尝试了解 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;
     }
 }

【问题讨论】:

  • 你做了什么来调试你的问题?您是否尝试过编写一个最小的工作示例?
  • 您确实调用了标准不支持的函数。

标签: c++ string function fstream ifstream


【解决方案1】:

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

如果你看一下at the reference,你会注意到复制构造函数是不允许的。

所以不要使用:

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

您应该使用引用调用:

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-02-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多