Sign in to follow this  
Quak

ofstream error

Recommended Posts

hi, i created an ofstream object in my class like this: std::ofstream os; if i try to open a file or do anything else with the object i get this error: i:\Projekte\Raytracer\Raytracer\application\logbook.h(20): error C2079: 'CLog::logbook' uses undefined class 'std::basic_ofstream<_Elem,_Traits>' with [ _Elem=char, _Traits=std::char_traits<char> ] i have all necessary includes defined i think. do you know what is going on ? thx Quak

Share this post


Link to post
Share on other sites
if you can post all your includes and your code to try to load a file, it'll make it much easier to help you. :)

also, if you could post logbook.h, which seems to be creating the problem.

Share this post


Link to post
Share on other sites
Quote:
Original post by Fruny
Quote:
Original post by Quak
the fstream.h fixed it. Thanks to you, shame on me :|


NOT <fstream.h> but <fstream> [attention]


Now I know the reason that they have fstream instead of fstream.h (fstream is newer), but what is the actual difference(not between the two include files, I know WHY) but, isn't #include <fstream> still pointing to a .h file, or is it including just a file named fstream, or is it something different.

Why doesn't M$ just make it that VS just has all the include files to be without .h or is there something else that I am missing.


Not really sure about it, but I was just wondering about it.

Edit: Better yet, can anyone just explain the difference between <fstream> and <fstream.h>,... not there reason for being seperate, but what the actual difference is between the naming (if you know what I mean), I don't even have <fstream.h> on VS 2005 Beta 2.

I'd search through the include directories myself, but I'm at college.

Share this post


Link to post
Share on other sites
Quote:
Original post by ChaosCommand
Quote:
Original post by Fruny
Quote:
Original post by Quak
the fstream.h fixed it. Thanks to you, shame on me :|


NOT <fstream.h> but <fstream> [attention]


Now I know the reason that they have fstream instead of fstream.h (fstream is newer), but what is the actual difference(not between the two include files, I know WHY) but, isn't #include <fstream> still pointing to a .h file, or is it including just a file named fstream, or is it something different.


Most of the time, fstream is indeed an actual file, and quite often includes multiple other files.

<fstream> provides the classes and functions you want within the namespace std, whereas IIRC <fstream.h> pollutes the global namespace. It was determined that this kind of behavior as default was probably a bad idea.

To prevent breaking existing applications that went like so:
#include <fstream.h>

int main () {
ifstream file( "pie.txt" );
...
}

fstream.h was marked depreciated (not sure if by the standard, but at least by one of Microsofts compilers) and <fstream> created, with everything in the std namespace. The above code now translates into:
#include <fstream>
using namespace std;

int main () {
ifstream file( "pie.txt" );
...
}
or:
#include <fstream>

int main () {
std::ifstream file( "pie.txt" );
...
}

Share this post


Link to post
Share on other sites
Yep. Now 'fstream' is the real file, and 'fstream.h' is a wrapper for backwards compatibility. For educational purposes, I post the (relevant) contents of fstream.h from my version of gcc (with some explanatory comments):


// At the beginning is a copy of all the standard GPL boilerplate. There is an
// include guard using the symbol "_BACKWARD_FSTREAM_H", and some cryptic
// comments right at the end that are probably intended for some maintainer :/

#include "backward_warning.h"
// That file basically just triggers a #warning directive, so that you get a
// compiler warning when you use these headers (unless you tell the compiler not
// to warn you about it).

#include <fstream> // Grab all of the real fstream stuff...

using std::filebuf;
using std::ifstream;
using std::ofstream;
using std::fstream;
using std::streampos;

// And then dump the important bits out of the std:: namespace into the global
// namespace.

#ifdef _GLIBCXX_USE_WCHAR_T
using std::wfilebuf;
using std::wifstream;
using std::wofstream;
using std::wfstream;
using std::wstreampos;
#endif

// And similarly for the wide char versions of those classes, if appropriate.

Share this post


Link to post
Share on other sites
Actually the difference between <fstream> and <fstream.h> are slightly more profound than that. The original iostream library shipped with CFront, the first C++ compiler. Its headers were named iostream.h, fstream.h and so on. Later, when C++ was standardized, a new version of the iostream library was created with templates, namespaces and, most importantly, different flags. For example, everyone's favorite flag nocreate disappeared. To ease transition, the standard's version of the library was included when you used <fstream> and compiler vendors could ship the old CFront style iostream library with the <fstream.h> header. Unfortunately, we're a bit after the transition stage now, so some compiler now a days just use using statements to pull the iostream members in the global namespace when you include <iostream.h>, <fstream> and so on. What this basically means is that given a random compiler you have no idea what's going to happen if you include <fstream.h> or what the difference between the two are. It could be the old style library or it could be the new style library. Or it may not compile at all. If you really care I can go into more detail on the difference between the CFront iostream library and the C++ standard iostream library, but it's really not important in the present day, since you may not even have the CFront iostream library available with a given compiler. Just remember the moral: always use the new style headers with new code.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this