Archived

This topic is now archived and is closed to further replies.

iostream classes problems

This topic is 5682 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Having lots of problems understand how exactly to use the iostream classes. I have a class, which does alot of reading of individual characters. I wanted to make this an abstract enough so that i could be reading from a location in memory... from a file..... etc. In my class, i have a member variable defined as such: std::istream m_Stream; however, in my Constructor for the class, i keep getting the following error: ''std::basic_istream<_Elem,_Traits>'' no appropriate default constructor available I''m using VC++ 7.0 (the .net one) ... and looking at the definition of istream ... is just a specialized basic_istream for char''s ... and there is no default constructor for basic_istream theres a constructor for taking a streambuf, and one that takes a std::_Uninitialized enumeration. MS''s documentation is very sketchy and i can''t seem to find much about using iostreams. When searching online, most every site i find says the istream SHOULD have a default constructor. Also... in VC++ 7.0 ... ifstream is NOT derived from istream ... ifstream is a specialization of basic_ifstream for char''s ... and basic_ifstream is derived from basic_istream. Should i be using basic_istream for my class member instead then?? ... so i could pass a ifstream or an istream object?? Are these classes all messed up from standard?? or am i just trying to use them wrong??

Share this post


Link to post
Share on other sites
Ok, found some documentation on MSDN, they dont list a default constructor for istream, but GNU C++'s version does. So i guess i can't use istream like i thought. I need my client to pass me a streambuf instead of an istream class. So i guess the real question now is, can i construct my istream member, with a NULL pointer and then change it later? Also, does the istream class destroy the streambuf when its destroyed?

Also, MSDN documentation that i found does say ifstream is suppose to be derived from istream, but just a simple program below generates an error.

int main()
{
ifstream mystream;
(istream)mystream;
}

error C2440: 'type cast' : cannot convert from 'std::ifstream' to 'std::istream'.

I take it, this is just an an incompatibility with MS's implemtnation of the STL classes or im missing something here?


[edited by - Merick on June 27, 2002 12:42:32 PM]

Share this post


Link to post
Share on other sites

  
#include <iostream>
#include <fstream>

using namespace std;

int main ()
{
ifstream s;
(istream) s;
return 0;
}

Works for me. MSVC 6.0 with SP5. Make sure you''ve got the latest service pack. Otherwise, your libraries may be screwed-up.

Share this post


Link to post
Share on other sites
im using VC++ 7.0, there are no current service packs for that. I'm thinking its a bug in the new .net libraries, but not sure. I've made a similar post on the MSDN's forums for VC++/STL, hoping to find out if this is a bug or intended change.

[edited by - Merick on June 27, 2002 1:49:53 PM]

Share this post


Link to post
Share on other sites
Wow, it''s early in the morning. That''s not a proper cast. Shouldn''t compile at all! =)

  
#include <iostream>
#include <fstream>

using namespace std;

int main ()
{
ifstream s;
istream *pfs = (istream*) &s;
return 0;
}


Much better. Casting only works with pointers and references, not with objects. The prior code tried to build an actual istream object from an ifstream (conversion ctor), rather than treat the ifstream as an istream.

I have a feeling that your class should have a pointer to an istream, not an istream object.

Share this post


Link to post
Share on other sites
Yep, if i typecast a pointer it works fine, but why is this?? If i used any other class it works. Like:

class A
{
public:
};

class B : public A
{
public:
};

int main()
{
B myB;
(A)myB;
}

compiles just fine for me. Whats different about istream and ifstream that prevents this same conversion?

EDIT:
would it be better to pass a pointer to an istream class in my constructor? or a pointer to a streambuf?? i'm not quite sure i understand how streambuf and istream work together to understand what would be the advantage of doing something one way or the other.

[edited by - Merick on June 27, 2002 2:31:09 PM]

Share this post


Link to post
Share on other sites
hehe nm, got that question answered on the MSDN. Apparently, MSVC 6.0 allowed accidently had a copy constructor for the istream class, which is what allows the (istream)s statement to compile. They removed it in 7.0, which is why i got the error

Share this post


Link to post
Share on other sites
Streambuf is more low-level, so I wouldn''t think you''d want that, but it depends on your app. What''s this stream?

If I were writing a library that had some method like "setDebugStream" that allowed a client to create a stream to a file, stringstream or even cout and pass that into the class to get debug info out of it, then I would definitely use a pointer to a ostream class. I''m still not sure exactly what you''re doing so I can''t say what''s write for you, but if your class needs input from an arbitrary stream, odds are you need a pointer to an istream.

Share this post


Link to post
Share on other sites
well, this is for a lexical analyzer for a compiler im writting, so i can compile material settings, rendering states, scene graphs.... etc from a text file. I want it to flexiable enough that i might even be able to use it to parse user input from a console, script commands in memory. Its pretty similar to your output example, only with input And from what you said, i think istream * would be sufficent .... thanks ... hehe.. now if i can only figure out a way to define my lexical analyzer states without having to create 100 state classes or a 100 table entries.... i''ll be set

Share this post


Link to post
Share on other sites