C++ 试图引用已删除的函数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23603735/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
attempting to reference a deleted function
提问by HymansonSD
I'm trying to learn about the fstream class and I'm having some trouble. I created a couple of txt files, one with a joke and the other with a punchline (joke.txt) and (punchline.txt) just for the sake of reading in and displaying content. I ask the user for the file name and if found it should open it up, clear the flags then read the content in. but I cant even test what it reads in because I'm currently getting errors regarding a deleted function but I don't know what that means
我正在尝试了解 fstream 课程,但遇到了一些麻烦。我创建了几个 txt 文件,一个带有笑话,另一个带有妙语 (joke.txt) 和 (punchline.txt),只是为了阅读和显示内容。我向用户询问文件名,如果找到它应该打开它,清除标志然后读入内容。但我什至无法测试它读入的内容,因为我目前收到有关已删除函数的错误,但我没有不知道那是什么意思
error 1:
错误 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
the second error is the exact same but for the 2nd function (displayLastLine())
第二个错误完全相同,但对于第二个函数(displayLastLine())
and error 3:
和错误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
and here's my code:
这是我的代码:
#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;
}
}
回答by Theolodis
You do call a deleted function, being the copy constructor of the class std::ifstream
.
您确实调用了删除的函数,它是类的复制构造函数std::ifstream
。
If you take a look at the referenceyou notice, that the copy constructor is not allowed.
如果您查看您注意到的引用,则不允许使用复制构造函数。
so instead of using:
所以而不是使用:
void displayAllLines(ifstream joke);
void displayLastLine(ifstream punchline);
you should work with calls by reference:
您应该通过引用处理调用:
void displayAllLines(ifstream& joke);
void displayLastLine(ifstream& punchline);
Using a reference will behave just like calling the method with a copy, but in fact you are operating on the original object instead of a new copy-constructed object. Keep that in mind for further use of the call by reference.
使用引用的行为就像使用副本调用方法一样,但实际上您是在操作原始对象而不是新的副本构造对象。请记住这一点,以便进一步使用通过引用调用。