Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/126.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 如何在块内使用std::fstream?_C++_Objective C Blocks_Objective C++_Ifstream - Fatal编程技术网

C++ 如何在块内使用std::fstream?

C++ 如何在块内使用std::fstream?,c++,objective-c-blocks,objective-c++,ifstream,C++,Objective C Blocks,Objective C++,Ifstream,我在试图从块中使用ifstream时遇到问题。(这是一个更大、更复杂的项目的一部分,因此我快速编写了一个包含相关部分的小源文件。) 如果我声明它没有\u块,我会得到: foo.cpp:6:24: error: call to implicitly-deleted copy constructor of 'std::ifstream' (aka 'basic_ifstream<char>') __block std::ifstream file("/tmp/

我在试图从块中使用
ifstream
时遇到问题。(这是一个更大、更复杂的项目的一部分,因此我快速编写了一个包含相关部分的小源文件。)

如果我声明它没有
\u块
,我会得到:

foo.cpp:6:24: error: call to implicitly-deleted copy constructor of
      'std::ifstream' (aka 'basic_ifstream<char>')
        __block std::ifstream file("/tmp/bar") ;
                              ^~~~
foo.cpp:8:3: error: call to implicitly-deleted copy constructor of
      'const std::ifstream' (aka 'const basic_ifstream<char>')
                file.rdbuf() ;
                ^~~~
                // rdbuf() and (presumably) other const functions

foo.cpp:9:3: error: member function 'close' not viable: 'this' argument has
      type 'const std::ifstream' (aka 'const basic_ifstream<char>'), but
      function is not marked const
                file.close() ;
                ^~~~
                // open(), close(), and (presumably) other non-const functions
foo.cpp:8:3:错误:调用的隐式删除副本构造函数
‘const std::ifstream’(也称为‘const basic_ifstream’)
rdbuf()文件;
^~~~
//rdbuf()和(大概)其他常量函数
foo.cpp:9:3:错误:成员函数“close”不可行:“this”参数无效
键入'const std::ifstream'(也称为'const basic_ifstream'),但
函数未标记为常量
file.close();
^~~~
//open()、close()和(大概)其他非常量函数
在块内部使用fstream的正确方法是什么

如果在块中使用堆栈的C++对象,如果它没有复制构造函数,则是错误的。 这是第一个错误-<代码>块需要复制

正如引文所述,一个选项是在堆上声明ifstream(
new
/
delete
)。。但这很混乱

其余的错误是简单的常量正确性错误。未声明
\u block
将对象作为常量副本导入,这是两个错误中的第一个错误,并且不能用于调用非常量函数,如
close

尝试切换到,看看它们是否缓解了这些问题。

来自

如果在块中使用堆栈的C++对象,如果它没有复制构造函数,则是错误的。 这是第一个错误-<代码>块需要复制

正如引文所述,一个选项是在堆上声明ifstream(
new
/
delete
)。。但这很混乱

其余的错误是简单的常量正确性错误。未声明
\u block
将对象作为常量副本导入,这是两个错误中的第一个错误,并且不能用于调用非常量函数,如
close


试着切换到,看看它们是否能缓解这些问题。

我不知道什么是objective-c块,但你能改用lambda吗?@Pubby我不知道什么是lambda,所以我改用objective-c风格块。XD这是
C++/CLI
而不是
C++
,对吗?不是标准C++@BlacklightShining的一部分,但是是的,正如pubby所说,lamdas是做扩展尝试做的事情的新的标准方式。我不知道objective-C块是什么,但是你能用lambdas代替吗?@pubby我不知道lambda是什么,所以我用objective-C风格的块代替。XD这是
C++/CLI
而不是
C++
,对吗?不是标准C++@BlacklightShining的一部分,但正如pubby所说,LAMDA是执行扩展尝试执行的操作的新标准方式。不声明
\u块
会在块打开时复制变量created@newacct已将其修复为const copy未声明
\u块
在块打开时复制变量created@newacct已将其修复为常量副本
foo.cpp:8:3: error: call to implicitly-deleted copy constructor of
      'const std::ifstream' (aka 'const basic_ifstream<char>')
                file.rdbuf() ;
                ^~~~
                // rdbuf() and (presumably) other const functions

foo.cpp:9:3: error: member function 'close' not viable: 'this' argument has
      type 'const std::ifstream' (aka 'const basic_ifstream<char>'), but
      function is not marked const
                file.close() ;
                ^~~~
                // open(), close(), and (presumably) other non-const functions