使用创建为类成员的fstream对象 [英] using fstream object created as class member

查看:224
本文介绍了使用创建为类成员的fstream对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在类中声明了一个fstream对象,如下所示(仅作为示例):

I have an fstream object declared in my class like this (just an example):

class Asd {

  public:
  Asd();

  private:
  std::fstream stream;

};

现在在调用构造函数时,我想像这样指定fstream参数

Now when the constructor is called I want to specify the fstream parameters like this

Asd::Asd() {

  this->stream = std::fstream(file, std::fstream::in);

}

然后在我拥有的所有类函数中使用该流,但无效。 VS给我的一个错误是:

and then use that stream in all class functions that I have, but it doesn't work. One error VS is giving me is:

no accessible path to private member declared in virtual base 'std::basic_ios<_Elem,_Traits>'

所以我仔细阅读了一下,所能找到的就是我做不到(或者更确切地说:不应该)复制流,实际上我什至不想这样做。有人说可以将其添加到构造函数中:

So I read up on that and all I could find is that I can't (or rather: shouldn't) copy a stream and indeed I don't even want to do that. Someone said one could add this to the constructor:

Asd::Asd() : stream(file, std::fstream::in) {

  ...

}

但是它显示了相同的错误,我不知道该怎么办...还有其他人说我必须引用该对象,但是我不知道怎么做?我只是想让它工作,但我无法弄清楚:(

but it prints the same error and I don't know what to do... Also someone else said I have to reference the object but I don't know how?? I just want this to work but I can't figure it out :(

编辑:这是完整的错误消息

this is the full error message

1>c:\program files (x86)\microsoft visual studio 10.0\vc\include\istream(860): error C2249: 'std::basic_ios<_Elem,_Traits>::operator =' : no accessible path to private member declared in virtual base 'std::basic_ios<_Elem,_Traits>'
1>          with
1>          [
1>              _Elem=char,
1>              _Traits=std::char_traits<char>
1>          ]
1>          c:\program files (x86)\microsoft visual studio 10.0\vc\include\ios(177) : see declaration of 'std::basic_ios<_Elem,_Traits>::operator ='
1>          with
1>          [
1>              _Elem=char,
1>              _Traits=std::char_traits<char>
1>          ]
1>          This diagnostic occurred in the compiler generated function 'std::basic_istream<_Elem,_Traits> &std::basic_istream<_Elem,_Traits>::operator =(const std::basic_istream<_Elem,_Traits> &)'
1>          with
1>          [
1>              _Elem=char,
1>              _Traits=std::char_traits<char>
1>          ]
1>c:\program files (x86)\microsoft visual studio 10.0\vc\include\ostream(604): error C2249: 'std::basic_ios<_Elem,_Traits>::operator =' : no accessible path to private member declared in virtual base 'std::basic_ios<_Elem,_Traits>'
1>          with
1>          [
1>              _Elem=char,
1>              _Traits=std::char_traits<char>
1>          ]
1>          c:\program files (x86)\microsoft visual studio 10.0\vc\include\ios(177) : see declaration of 'std::basic_ios<_Elem,_Traits>::operator ='
1>          with
1>          [
1>              _Elem=char,
1>              _Traits=std::char_traits<char>
1>          ]
1>          This diagnostic occurred in the compiler generated function 'std::basic_ostream<_Elem,_Traits> &std::basic_ostream<_Elem,_Traits>::operator =(const std::basic_ostream<_Elem,_Traits> &)'
1>          with
1>          [
1>              _Elem=char,
1>              _Traits=std::char_traits<char>
1>          ]


推荐答案

根据您所写的内容,您似乎仍然在构造函数中有一个赋值。尝试这样做是否可行:

Based on what you write, it seems you still have an assignment in your constructor. Try if this works:

class Asd {
public:
    Asd(char const* file);

private:
    Asd(Asd&);
    void operator= (Asd&);

    std::ifstream stream;
};

Asd::Asd(char const* file): stream(file) {}

使用成员初始化程序列表构造对象避免了默认构造它,然后必须稍后对其进行设置。如果您确实要先构造流并在以后进行设置,则不能使用分配,因为流既不能复制构造,也不能复制分配。但是,您可以打开它,例如:

Using the member initializer list to construct the object avoids default constructing it and then having to set it up later. If you really want to first construct the stream and set it up later, you can't use assignment because streams are neither copy constructible nor copy assignable. However, you could just open it, e.g.:

Asd::Asd(char const* file) { stream.open(file); }

通过使用 std :: ifstream 它不需要传递 std :: ios_base :: in (或通过间接从 std ::派生的类获取此值的任何其他变体: ios_base ,例如 std :: fstream )到构造函数或 open() std :: ios_base :: in 会自动添加到传递给 std :: ifstream 的构造函数或 std :: ifstream :: open()。另外, std :: ifstream 是一个比 std :: fstream 更简单的类。

By using std::ifstream it isn't needed to pass std::ios_base::in (or any other variation at getting a this value through classes indirectly derived from std::ios_base like std::fstream) to the constructor or to open(): std::ios_base::in is automatically added to whatever are passed to std::ifstream's constructor or to std::ifstream::open(). Also, std::ifstream is a somewhat simpler class than std::fstream. It should be smaller and is probably faster to construct.

基于Mooing Duck的评论,我添加了一个私有副本构造和一个私有赋值运算符,希望编译器如果您尝试复制构造或复制分配 Asd 对象,将指向您其中之一。请注意,将传递给采用按值作为参数的函数或返回 Asd 对象时,将尝试复制对象。在构造函数中的赋值很可能是编译器抱怨无法复制流的一个地方,但是可能在其他地方进行了复制尝试。

Based on Mooing Duck's comment I have added a private copy construct and a private assignment operator, in the hope that the compiler will point you at one of these in case you try to copy construct or copy assign an Asd object. Note that an attempt is made copy object when passing the to a function taking the argument by value or when returning an Asd object. It is quite possible that the assignment in your constructor was one place where the compiler complained about not being able to copy a stream but there may be other places where a copy attempt is made.

这篇关于使用创建为类成员的fstream对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆