C++ 无法引用函数,因为它是已删除的函数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25827924/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 11:26:28  来源:igfitidea点击:

Function cannot be referenced as it is a deleted function

c++

提问by Foysal94

Hello I am learning C++ from a book and am on a exercise question below

您好,我正在从一本书中学习 C++,并且正在回答下面的练习题

Write a function that takes and returns an istream&. The function should read the stream until it hits end-of-file. The function should print what it reads to the standard output. Reset the stream so that it is valid before returning the stream.

编写一个接受并返回 istream& 的函数。该函数应该读取流,直到它到达文件结尾。该函数应该将它读取的内容打印到标准输出。在返回流之前重置流以使其有效。

#include "stdafx.h"
#include <iostream>
#include <istream>
#include <string>
#include <string.h>
#include <list>
#include <vector>
#include <fstream>

std::istream ReadFile(std::istream &iStream)
{
    std::string word;
    while (iStream >> word)
    {}
    std::cout << "I read value " << word << std::endl;
    iStream.setstate(std::ios::goodbit);
    return iStream;

}

int _tmain(int argc, _TCHAR* argv[])
{
    ReadFile(std::cin);

    system("pause");
    return 0;
}

The above is my attempt, however I am getting errors at the "return iStream" line.

以上是我的尝试,但是我在“返回 iStream”行出现错误。

Error1 error C2280: 'std::basic_istream<char,std::char_traits<char>>::basic_istream(const std::basic_istream<char,std::char_traits<char>> &)' : attempting to reference a deleted function  

2 IntelliSense: function "std::basic_istream<_Elem, _Traits>::basic_istream(const std::basic_istream<_Elem, _Traits>::_Myt &) [with _Elem=char, _Traits=std::char_traits<char>]" (declared at line 77 of "C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\include\istream") cannot be referenced -- it is a deleted function

I don't really know what these errors are, I am aware you can delete stuff but I am not onto that topic in the book yet. As far as I know I have not at all touched the istream file... Can someone help me please?

我真的不知道这些错误是什么,我知道你可以删除内容,但我还没有在书中讨论这个主题。据我所知,我根本没有接触过 istream 文件......有人可以帮我吗?

Thanks!

谢谢!

回答by Cheers and hth. - Alf

You can’t return an istreamby value because it’s not copyable.

您不能返回istreamby 值,因为它不可复制。

Since it’s not copyable the copy constructor has been deleted (to enforce the non-copyability), and that’s the direct technical cause of the diagnostic.

由于它不可复制,复制构造函数已被删除(以强制执行不可复制性),这是诊断的直接技术原因。

So, instead of

所以,而不是

std::istream ReadFile(std::istream &iStream)

… do

… 做

std::istream& ReadFile(std::istream& iStream)

In other news, …

在其他新闻中,...



Instead of

代替

#include "stdafx.h"

just turn off precompiled headers in the Visual Studio project settings.

只需在 Visual Studio 项目设置中关闭预编译头即可。

This also gives you more standard-conforming behavior for header inclusions.

这也为您提供了更多符合标准的标题包含行为。

If you don’t do that, then configure the project so that any warning about skipping an include, yields a hard compilation error.

如果您不这样做,请配置项目,以便任何有关跳过包含的警告都会产生硬编译错误。



Instead of

代替

iStream.setstate(std::ios::goodbit);

… do

… 做

istream.clear();


Instead of the non-portable Microsoft monstrosity

而不是不可移植的微软怪物

int _tmain(int argc, _TCHAR* argv[])

just use standard

只需使用标准

int main()

or in C++11 trailing return type syntax,

或在 C++11 尾随返回类型语法中,

auto main() -> int


Instead of

代替

system("pause");

simply run your program via Ctrl+F5in Visual Studio. Or, place a breakpoint on the last right brace of mainand run in the debugger. Or, run the program from the command line.

只需在 Visual Studio 中通过Ctrl+运行您的程序F5。或者,main在调试器的最后一个右大括号上放置一个断点并在调试器中运行。或者,从命令行运行程序。



The exercise formulation

运动公式

should read the stream until it hits end-of-file

应该读取流直到它到达文件结尾

is ambiguous, but anyway reading words, as you’re doing, does not faithfully reproduce whitespace in the stream. For a more accurate reproduction of the stream contents you can either read characterby character, or (via getline) lineby line. Or, you can use a special mechanism for this task, namely outputting the read buffer, which does everything in one little statement.

是模棱两可的,但无论如何阅读words,就像你在做的那样,并不能忠实地再现流中的空白。用于流内容的更准确的再现既可以读字符由字符,或(通过getline线由线。或者,您可以为此任务使用一种特殊机制,即输出读取缓冲区,它在一个小语句中完成所有操作。



Finally, you don’t need all those headers. You only need <iostream>, and if you choose to read lines, also <string>. Also, you don’t need the return 0;at the end of main, because that’s the default.

最后,您不需要所有这些标题。您只需要<iostream>,如果您选择阅读行,还需要<string>. 此外,您不需要return 0;末尾的main,因为这是默认设置。

回答by nneonneo

A deleted functionis a special function (constructor, destructor, operator) that has been explicitly disabled. If you look carefully at the error you can see that the function is the basic_istreamcopy-constructor, which is disabled because istreams cannot be copied. You are attempting to copy the istream when you return istream, since your function is declared as returning an istream(rather than e.g. returning a reference to an istream).

一个删除函数是一个特殊的函数(构造函数,析构函数,运营商)已被明确禁用。如果仔细查看错误,您会发现该函数是basic_istream复制构造函数,由于无法复制 istreams,该函数被禁用。当您尝试复制 istream 时return istream,因为您的函数被声明为返回 an istream(而不是例如返回对 an 的引用istream)。