C++ ‘ofstream’ does not name a type
Hi
I am working on an OpenSuse 10.2 Station. I am trying to write a code using ofstream as a global variable - in a header file. Here is basicly what I've wrote and what went wrong: @ x.h Code:
extern ofstream myfile; Code:
ofstream myfile("powlev.txt", ios::out); Code:
#include "x.h" x.h:1: error: ‘ofstream’ does not name a type I am new to this kind of file writing method so please, can anyone help? Obviously there is something wrong but it seems I can't find so many title about this word associacion: " ‘ofstream’ does not name a type " Thanks a bunch!! Liah |
C++ ‘ofstream’ does not name a type
I did make it work finally...
I just changed the place of the declaration. @x.h Code:
void initialize(); Code:
#include "x.h" Code:
#include "x.h" See ya! |
I would actually put the standard headers before your own headers, like:
Quote:
Arch Linux |
need to include iostream in the header
it can be resolved by including following line in the header file :
using namespace std; Hope it works for you. |
Quote:
The original issue is that the OP put a line in x.h that won't compile unless iostream and/or fstream precedes that line. The OP's answer was to move that problem line out of x.h and put it in a .c file where the required headers have already been read. That work's but it isn't a good answer. Your answer was change the include order so that any .c that includes x.h includes iostream and fstream first. I think that answer is even worse. If x.h includes any line what won't compile without iostream and fstream, then x.h itself should include iostream and fstream. One of the coding standards where I work is that every .hpp file is coded so that it can compile even if it is the first thing included by some .cpp. That means a lot of .hpp files reinclude the same headers that other .hpp files have already included. But include guards work well to deal with that, and the alternative of a lot of complicated rules for include sequence is worse. |
All times are GMT -5. The time now is 08:55 PM. |