Hi. I need to use a fstream variable for each instance of my class but...

#include <fstream>
class MyClass {
public:
MyClass(bool bKernel) {
}
std::fstream fichier;
};
int main() {
MyClass module(false);
module = new MyClass(true);
return 0;
}

This one wont work printing some weird errors :

g++ -O0 -g3 -Wall -c -fmessage-length=0 -MMD -MP -MF"toto.d" -MT"toto.d" -o"toto.o" "../toto.cpp"
/usr/lib/gcc/i486-linux-gnu/4.1.2/../../../../include/c++/4.1.2/bits/ios_base.h: In member function ‘std::basic_ios<char, std::char_traits<char> >& std::basic_ios<char, std::char_traits<char> >::operator=(const std::basic_ios<char, std::char_traits<char> >&)’:
/usr/lib/gcc/i486-linux-gnu/4.1.2/../../../../include/c++/4.1.2/bits/ios_base.h:782: error: ‘std::ios_base& std::ios_base::operator=(const std::ios_base&)’ is private
/usr/lib/gcc/i486-linux-gnu/4.1.2/../../../../include/c++/4.1.2/iosfwd:55: error: within this context
/usr/lib/gcc/i486-linux-gnu/4.1.2/../../../../include/c++/4.1.2/iosfwd: In member function ‘std::basic_istream<char, std::char_traits<char> >& std::basic_istream<char, std::char_traits<char> >::operator=(const std::basic_istream<char, std::char_traits<char> >&)’:
/usr/lib/gcc/i486-linux-gnu/4.1.2/../../../../include/c++/4.1.2/iosfwd:61: note: synthesized method ‘std::basic_ios<char, std::char_traits<char> >& std::basic_ios<char, std::char_traits<char> >::operator=(const std::basic_ios<char, std::char_traits<char> >&)’ first required here
/usr/lib/gcc/i486-linux-gnu/4.1.2/../../../../include/c++/4.1.2/iosfwd: In member function ‘std::basic_iostream<char, std::char_traits<char> >& std::basic_iostream<char, std::char_traits<char> >::operator=(const std::basic_iostream<char, std::char_traits<char> >&)’:
/usr/lib/gcc/i486-linux-gnu/4.1.2/../../../../include/c++/4.1.2/iosfwd:67: note: synthesized method ‘std::basic_istream<char, std::char_traits<char> >& std::basic_istream<char, std::char_traits<char> >::operator=(const std::basic_istream<char, std::char_traits<char> >&)’ first required here
/usr/lib/gcc/i486-linux-gnu/4.1.2/../../../../include/c++/4.1.2/iosfwd: In member function ‘std::basic_fstream<char, std::char_traits<char> >& std::basic_fstream<char, std::char_traits<char> >::operator=(const std::basic_fstream<char, std::char_traits<char> >&)’:
/usr/lib/gcc/i486-linux-gnu/4.1.2/../../../../include/c++/4.1.2/iosfwd:95: note: synthesized method ‘std::basic_iostream<char, std::char_traits<char> >& std::basic_iostream<char, std::char_traits<char> >::operator=(const std::basic_iostream<char, std::char_traits<char> >&)’ first required here
/usr/lib/gcc/i486-linux-gnu/4.1.2/../../../../include/c++/4.1.2/streambuf: In member function ‘std::basic_filebuf<char, std::char_traits<char> >& std::basic_filebuf<char, std::char_traits<char> >::operator=(const std::basic_filebuf<char, std::char_traits<char> >&)’:
/usr/lib/gcc/i486-linux-gnu/4.1.2/../../../../include/c++/4.1.2/streambuf:789: error: ‘std::basic_streambuf<_CharT, _Traits>& std::basic_streambuf<_CharT, _Traits>::operator=(const std::basic_streambuf<_CharT, _Traits>&) [with _CharT = char, _Traits = std::char_traits<char>]’ is private
/usr/lib/gcc/i486-linux-gnu/4.1.2/../../../../include/c++/4.1.2/iosfwd:86: error: within this context
/usr/lib/gcc/i486-linux-gnu/4.1.2/../../../../include/c++/4.1.2/iosfwd: In member function ‘std::basic_fstream<char, std::char_traits<char> >& std::basic_fstream<char, std::char_traits<char> >::operator=(const std::basic_fstream<char, std::char_traits<char> >&)’:
/usr/lib/gcc/i486-linux-gnu/4.1.2/../../../../include/c++/4.1.2/iosfwd:95: note: synthesized method ‘std::basic_filebuf<char, std::char_traits<char> >& std::basic_filebuf<char, std::char_traits<char> >::operator=(const std::basic_filebuf<char, std::char_traits<char> >&)’ first required here
../toto.cpp: In member function ‘MyClass& MyClass::operator=(const MyClass&)’:
../toto.cpp:3: note: synthesized method ‘std::basic_fstream<char, std::char_traits<char> >& std::basic_fstream<char, std::char_traits<char> >::operator=(const std::basic_fstream<char, std::char_traits<char> >&)’ first required here
../toto.cpp: In function ‘int main()’:
../toto.cpp:15: note: synthesized method ‘MyClass& MyClass::operator=(const MyClass&)’ first required here
make: *** [toto.o] Erreur 1

But if I declare the fstream outside of the class' scope, it will work :

#include <fstream>
std::fstream fichier;
class MyClass {
public:
MyClass(bool bKernel) {
}
};
int main() {
MyClass module(false);
module = new MyClass(true);
return 0;
}

Can you help me about that ?

This: module = new MyClass(true); line should not work in either case. module is of type MyClass, but new MyClass(true) is of type MyClass*. That assignment shouldn't work - are you sure the latter example compiles?

Also, use [code]

[/code] tags, not [quote][/quote] tags.

The main problem is that iostreams cannot be copied. Consider for a moment the horrors that can occur if you have three copies of an object that manipulates a file sitting around.

You must pass them by [i]reference[/i].

So, if you want your class to have a fstream, you should either make the class uncopyable or give it a reference-counted pointer to a dynamically-allocated fstream.

The [URL="http://www.boost.org/doc/libs/1_35_0/libs/smart_ptr/smart_ptr.htm"]Boost Library's Smart Pointer[/URL] classes may be of use to you. In particular, use
[URL="http://www.boost.org/doc/libs/1_35_0/libs/smart_ptr/shared_ptr.htm"]shared_ptr[/URL]<fstream> fichier;
in your class. When you initialize the class, or open the file (whichever), construct yourself a new fstream
[inlinecode]fichier = new fstream( "essai.txt" );[/inlinecode]
Thereafter, you can copy your class and each [i]copy[/i] will have a pointer to the [i]same[/i] fstream, and you won't have to worry about [b]delete[/b]ing it at the appropriate time. And, of course, distinct instances of your class (not copies) access different files.

Hope this helps.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.