求助:有关ifstream

chinson 2005-12-22 09:43:33
我有段代码:

class myClass{
private:
ifstream ifile;
public:
myClass(ifstream & file){ifile = file};//这里有错误
};
int main(int argc, char **argv)
{
string file = "input.txt";
ifstream ifile(file.c_str());
myClass myclass(ifile);
return 0;
}

用dev-c++编译
编译错误信息是:
'std::ios_base& std::ios_base::operator=(const std::ios_base&)' is private

请问有遇到过这个问题吗?怎么解决的。

谢谢
...全文
220 16 打赏 收藏 转发到动态 举报
写回复
用AI写文章
16 条回复
切换为时间正序
请发表友善的回复…
发表回复
shenmea00000 2005-12-25
  • 打赏
  • 举报
回复
学习中~~~~~~~~~~~~~~~~~~~~~~
chinson 2005-12-23
  • 打赏
  • 举报
回复
哦,可能是我理解有误。
我以为这样是防止多个对象指向同一个流,有可能造成线程不安全。
whyglinux 2005-12-22
  • 打赏
  • 举报
回复
>> 但是用指针或引用解决这个问题后,难道这不算分流吗?

不算。因为它们实际上表示的同一个流对象,所以只有一个流。
chinson 2005-12-22
  • 打赏
  • 举报
回复
to whyglinux:
再问一句,你说输入输出流是单向流,而且不能“分流”。
但是用指针或引用解决这个问题后,难道这不算分流吗?
chinson 2005-12-22
  • 打赏
  • 举报
回复
多谢多谢
whyglinux 2005-12-22
  • 打赏
  • 举报
回复
>> 你的意思是不应该这么做?

是的。不过,你可以从另外的角度考虑实现,比如在类中定义一个流对象的指针或者引用等,从而可避免对流对象的拷贝。
sinall 2005-12-22
  • 打赏
  • 举报
回复
你可以在CSDN搜索一下,看istream都提供什么构造函数。
explicit basic_istream(basic_streambuf<E, T> *sb);
而,ifstream有找个函数:
basic_filebuf<E, T> *rdbuf() const;
sinall 2005-12-22
  • 打赏
  • 举报
回复
#include <iostream>
#include <fstream>
using namespace std;

class myClass{
private:
istream input; //注意这里,是istream,而不是ifstream。
public:
myClass(istream & file)
: input(file.rdbuf())
{};
};
int main(int argc, char **argv)
{
string file = "input.txt";
ifstream ifile(file.c_str());
myClass myclass(ifile);
return 0;
}
chinson 2005-12-22
  • 打赏
  • 举报
回复
to sinall:
谢谢你。不过你的这个方法我也想到过。

to all:
我得本意是:
class myClass{
private:
istream input; //注意这里,是istream,而不是ifstream。
public:
myClass(istream & file){ifile = file};
};
int main(int argc, char **argv)
{
string file = "input.txt";
ifstream ifile(file.c_str());
myClass myclass(ifile);
return 0;
}

按照whyglinux(山青水秀)的解释,这样就没有办法吗?
晨星 2005-12-22
  • 打赏
  • 举报
回复
同意 whyglinux(山青水秀)。我觉得也是为了降低复杂性吧,允许两个对象代表两个同样的流会引出好多复杂的问题。
所以基类中吧operator=以及拷贝构造函数都私有化了,于是,从外部,你既无法做拷贝赋值,也无法做拷贝构造。所以那两个都是不行的。
lcd5 2005-12-22
  • 打赏
  • 举报
回复
ifstream 没有拷贝构造函数和赋值操作符重载
chinson 2005-12-22
  • 打赏
  • 举报
回复
to whyglinux:
你的意思是不应该这么做?

ps:好像在vc下编译没有问题
sinall 2005-12-22
  • 打赏
  • 举报
回复
我上面说错了。
编译错误的原因在ifstream的赋值函数和拷贝构造函数都不是public,所以无法访问。
你的myClass可以按照下面的方法定义:

#include <iostream>
#include <fstream>
using namespace std;

class myClass{
private:
ifstream ifile;
public:
myClass(const char *s, ios_base::openmode mode = ios_base::in)
: ifile(s, mode)
{};//这里有错误
};
int main(int argc, char **argv)
{
string file = "input.txt";
ifstream ifile(file.c_str());
myClass myclass(file.c_str());
return 0;
}
chinson 2005-12-22
  • 打赏
  • 举报
回复
我试过了,也是同样的错误
whyglinux 2005-12-22
  • 打赏
  • 举报
回复
在C++中的输入输出流是单向流,而且不能“分流”,即不能进行拷贝。为此目的,在流类中赋值拷贝函数被设置为私有的,禁止使用;而且也没有用流对象来初始化的构造函数。
sinall 2005-12-22
  • 打赏
  • 举报
回复
用初始化列表呢?

class myClass{
private:
ifstream ifile;
public:
myClass(ifstream & file)
: ifile(file)
{};//这里有错误
};
int main(int argc, char **argv)
{
string file = "input.txt";
ifstream ifile(file.c_str());
myClass myclass(ifile);
return 0;
}

64,637

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧