using fstream object created as class member

帅比萌擦擦* 提交于 2019-12-11 06:24:57

问题


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

class Asd {

  public:
  Asd();

  private:
  std::fstream stream;

};

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);

}

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 :(

Edit: 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>          ]

回答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); }

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.

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.



来源:https://stackoverflow.com/questions/18522108/using-fstream-object-created-as-class-member

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!