FSTREAM类成员变量
fstream class member variable
我花了几个小时在较大的代码中跟踪错误。我将其压缩到一个小文件中。我需要将Fstream用作清洁代码的模因变量。在线资源说这应该有效。我还尝试使用.open()初始化fstream,但没有成功。我正在用G 编译Ubuntu 16.04。
#include <string>
#include <fstream>
#include <iostream>
using namespace std;
class read{
private:
ifstream infile;
public:
read(string fileName): infile(fileName.c_str());}
~read(){infile.close();}
};
int main(){
string fileName = "./test/FileCreator/SourceTEST.cpp";
read r = read(fileName);
return 0;
}
编译器错误
/usr/include/c++/5/bits/ios_base.h: In copy constructor ‘std::basic_ios<char>::basic_ios(const std::basic_ios<char>&)’:
/usr/include/c++/5/bits/ios_base.h:855:5: error: ‘std::ios_base::ios_base(const std::ios_base&)’ is private
ios_base(const ios_base&);
^
In file included from /usr/include/c++/5/ios:44:0,
from /usr/include/c++/5/istream:38,
from /usr/include/c++/5/fstream:38,
from smallTestRead.cpp:2:
/usr/include/c++/5/bits/basic_ios.h:67:11: error: within this context
class basic_ios : public ios_base
^
In file included from smallTestRead.cpp:2:0:
/usr/include/c++/5/fstream: In copy constructor ‘std::basic_ifstream<char>::basic_ifstream(const std::basic_ifstream<char>&)’:
/usr/include/c++/5/fstream:455:11: note: synthesized method ‘std::basic_ios<char>::basic_ios(const std::basic_ios<char>&)’ first required here
class basic_ifstream : public basic_istream<_CharT, _Traits>
^
In file included from /usr/include/c++/5/ios:43:0,
from /usr/include/c++/5/istream:38,
from /usr/include/c++/5/fstream:38,
from smallTestRead.cpp:2:
/usr/include/c++/5/streambuf: In copy constructor ‘std::basic_filebuf<char>::basic_filebuf(const std::basic_filebuf<char>&)’:
/usr/include/c++/5/streambuf:804:7: error: ‘std::basic_streambuf<_CharT, _Traits>::basic_streambuf(const std::basic_streambuf<_CharT, _Traits>&) [with _CharT = char; _Traits = std::char_traits<char>]’ is private
basic_streambuf(const basic_streambuf&);
^
In file included from smallTestRead.cpp:2:0:
/usr/include/c++/5/fstream:72:11: error: within this context
class basic_filebuf : public basic_streambuf<_CharT, _Traits>
^
/usr/include/c++/5/fstream: In copy constructor ‘std::basic_ifstream<char>::basic_ifstream(const std::basic_ifstream<char>&)’:
/usr/include/c++/5/fstream:455:11: note: synthesized method ‘std::basic_filebuf<char>::basic_filebuf(const std::basic_filebuf<char>&)’ first required here
class basic_ifstream : public basic_istream<_CharT, _Traits>
^
smallTestRead.cpp: In copy constructor ‘read::read(const read&)’:
smallTestRead.cpp:7:7: note: synthesized method ‘std::basic_ifstream<char>::basic_ifstream(const std::basic_ifstream<char>&)’ first required here
class read{
^
smallTestRead.cpp: In function ‘int main()’:
smallTestRead.cpp:17:24: note: synthesized method ‘read::read(const read&)’ first required here
read r = read(fileName);
read r = read(fileName);
在C 11之前的c 版本上首先创建一个未命名的class read
实例,然后使用复制构造函数将其复制到r
中。C 的标准IO流是不可复制的,这使得不可复制。因此,您尝试使用复制构造函数的错误。
c 11上方的版本将使用移动构造函数,该构造器将使该代码有效,因为标准IO流是可移动但不可复制的。使用read r(fileName);
将阻止所有版本使用任何一个构造函数,而是将r
构造到位。
流对象不可复制,因此您不能说:
read r = read(fileName);
对于包含流对象的read
对象。另外,这是:
read(string fileName): infile(fileName.c_str());}
应该是:
read(string fileName): infile(fileName.c_str()) {}
相关文章:
- 将成员变量添加到共享库中的类中,不会破坏二进制兼容性吗
- 具有奇怪重复模板模式的派生类中的成员变量已损坏
- C++预处理会生成变量成员、资源库和映射
- 局部堆栈变量成员的返回值优化
- Google Mock:在目标类的构造函数中实例化的模拟私有变量成员
- 使用指向结构变量成员的指针访问该结构的成员的地址
- 如果派生类仅包含自动变量成员,是否有必要具有虚拟驱动器
- 线程安全性和静态变量/成员功能
- "static const char array"可以在 C 语言上包含变量成员吗
- 仅用于内部目的的类的所有变量/成员的技术术语是什么
- 如何强制转换变量成员以将其作为函数的引用参数传递
- 类中未声明变量成员函数
- 在 main 中初始化类的 "static const" 类型变量成员的更好方法
- c++模板类静态const变量成员作为映射键给出未定义引用
- 在类中初始化结构变量成员会导致分割错误
- 在c++中建模变量成员类型
- 模板私有静态变量成员的未定义符号
- 不能访问公共静态变量成员
- 常量变量成员在C++有什么用?
- g++ 4.8.2坚持简单变量成员是数组