IOStream and STL

So recently, I ran into an error while writing a program of mine, and it was a curious little error indeed.

Basically, I had a deque of objects that used ifstreams, and I couldn't expand the deque using push_back(), push_front(), or insert().

Upon reading the logs, I figured out that the problem revolved around that fact that the = operator for ios_base is private, as is what appeared to be the copy constructor, which those functions appeared to need.

I solved that problem without any further problems using a method not worth discussing, but that issue got me thinking. Out of curiosity, I wrote this little test program.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
//STL container w/ ifstream test.
#include <fstream>
#include <deque>

using namespace std;

int main()
{
	ifstream crabbe;
	ifstream goyle;
        deque<ifstream> draco_malfoy;
	crabbe.open("can.of.worms");
	goyle.open("sesame.treasuretrove");
	draco_malfoy.push_back(crabbe);
	draco_malfoy.push_back(goyle);
	return 0;
}


This program's compilation failed with a resounding set of errors. You can replace all instances of "deque" with "vector" or "list" and it won't fix the problem (in fact, vectors generated 8, not 4 errors).
Build testpad2 of project testpad2 with configuration Debug

CompileC build/testpad2.build/Debug/testpad2.build/Objects-normal/x86_64/main.o main.cpp normal x86_64 c++ com.apple.compilers.gcc.4_2
cd /Users/albatross/Documents/testpad2
setenv LANG en_US.US-ASCII
#There were a lot of parameters here.

/Developer/SDKs/MacOSX10.6.sdk/usr/include/c++/4.2.1/bits/ios_base.h: In copy constructor 'std::basic_ios<char, std::char_traits<char> >::basic_ios(const std::basic_ios<char, std::char_traits<char> >&)':
/Developer/SDKs/MacOSX10.6.sdk/usr/include/c++/4.2.1/iosfwd:55:   instantiated from 'void __gnu_cxx::new_allocator<_Tp>::construct(_Tp*, const _Tp&) [with _Tp = std::basic_ifstream<char, std::char_traits<char> >]'
/Developer/SDKs/MacOSX10.6.sdk/usr/include/c++/4.2.1/bits/stl_deque.h:1058:   instantiated from 'void std::deque<_Tp, _Alloc>::push_back(const _Tp&) [with _Tp = std::basic_ifstream<char, std::char_traits<char> >, _Alloc = std::allocator<std::basic_ifstream<char, std::char_traits<char> > >]'
/Users/albatross/Documents/testpad2/main.cpp:14:   instantiated from here
/Developer/SDKs/MacOSX10.6.sdk/usr/include/c++/4.2.1/bits/ios_base.h:779: error: 'std::ios_base::ios_base(const std::ios_base&)' is private
/Developer/SDKs/MacOSX10.6.sdk/usr/include/c++/4.2.1/iosfwd:55: error: within this context
/Developer/SDKs/MacOSX10.6.sdk/usr/include/c++/4.2.1/iosfwd: In copy constructor 'std::basic_ifstream<char, std::char_traits<char> >::basic_ifstream(const std::basic_ifstream<char, std::char_traits<char> >&)':
/Developer/SDKs/MacOSX10.6.sdk/usr/include/c++/4.2.1/iosfwd:89: note: synthesized method 'std::basic_ios<char, std::char_traits<char> >::basic_ios(const std::basic_ios<char, std::char_traits<char> >&)' first required here 
/Developer/SDKs/MacOSX10.6.sdk/usr/include/c++/4.2.1/streambuf: In copy constructor 'std::basic_filebuf<char, std::char_traits<char> >::basic_filebuf(const std::basic_filebuf<char, std::char_traits<char> >&)':
/Developer/SDKs/MacOSX10.6.sdk/usr/include/c++/4.2.1/streambuf:794: error: 'std::basic_streambuf<_CharT, _Traits>::basic_streambuf(const std::basic_streambuf<_CharT, _Traits>&) [with _CharT = char, _Traits = std::char_traits<char>]' is private
/Developer/SDKs/MacOSX10.6.sdk/usr/include/c++/4.2.1/iosfwd:86: error: within this context
/Developer/SDKs/MacOSX10.6.sdk/usr/include/c++/4.2.1/iosfwd: In copy constructor 'std::basic_ifstream<char, std::char_traits<char> >::basic_ifstream(const std::basic_ifstream<char, std::char_traits<char> >&)':
/Developer/SDKs/MacOSX10.6.sdk/usr/include/c++/4.2.1/iosfwd:89:   instantiated from 'void __gnu_cxx::new_allocator<_Tp>::construct(_Tp*, const _Tp&) [with _Tp = std::basic_ifstream<char, std::char_traits<char> >]'
/Developer/SDKs/MacOSX10.6.sdk/usr/include/c++/4.2.1/bits/stl_deque.h:1058:   instantiated from 'void std::deque<_Tp, _Alloc>::push_back(const _Tp&) [with _Tp = std::basic_ifstream<char, std::char_traits<char> >, _Alloc = std::allocator<std::basic_ifstream<char, std::char_traits<char> > >]'
/Users/albatross/Documents/testpad2/main.cpp:14:   instantiated from here
/Developer/SDKs/MacOSX10.6.sdk/usr/include/c++/4.2.1/iosfwd:89: note: synthesized method 'std::basic_filebuf<char, std::char_traits<char> >::basic_filebuf(const std::basic_filebuf<char, std::char_traits<char> >&)' first required here 
/Developer/SDKs/MacOSX10.6.sdk/usr/include/c++/4.2.1/ext/new_allocator.h: In member function 'void __gnu_cxx::new_allocator<_Tp>::construct(_Tp*, const _Tp&) [with _Tp = std::basic_ifstream<char, std::char_traits<char> >]':
/Developer/SDKs/MacOSX10.6.sdk/usr/include/c++/4.2.1/ext/new_allocator.h:107: note: synthesized method 'std::basic_ifstream<char, std::char_traits<char> >::basic_ifstream(const std::basic_ifstream<char, std::char_traits<char> >&)' first required here 


Has anyone else had problems using STL containers with ifstream?
Do we have any idea why they made it so that you cannot directly use an ifstream as a type for a vector, deque, or list?
Or... is there a way?

-Albatross
Last edited on
You can't copy fstreams.

/Developer/SDKs/MacOSX10.6.sdk/usr/include/c++/4.2.1/bits/ios_base.h:779: error: 'std::ios_base::ios_base(const std::ios_base&)' is private


That's the copy constructor for one of the base classes.
You can't copy fstreams.

Egh. Thought so. All well, I was hopeful.

-Albatross
maybe you need to catch the exception properly...

1
2
3
4
5
6
7
8
try {  	
      draco_malfoy.push_back(crabbe);
      draco_malfoy.push_back(goyle);


} catch (death::eaters)  {
   //
}

you should try using

Boost::Wizard::Dumbledore
Topic archived. No new replies allowed.