LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   C++ ‘ofstream’ does not name a type (https://www.linuxquestions.org/questions/linux-newbie-8/c-%91ofstream%92-does-not-name-a-type-710786/)

Liah 03-11-2009 11:07 AM

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;
@x.c
Code:

ofstream myfile("powlev.txt", ios::out);
@example.c
Code:

#include "x.h"
#include <iostream>
#include <fstream>

void main()
{
  myfile << "Say something!!\n";

  myfile.close();
{

When I compile the program I keep getting :
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

Liah 03-11-2009 11:51 AM

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();
@x.c
Code:

#include "x.h"
#include <iostream>
#include <fstream>

ofstream myfile;

void initialize()
{
  myfile.open("powlev.txt", ios::out);
}

@example.c
Code:

#include "x.h"
#include <iostream>
#include <fstream>

void main()
{
  myfile << "Say something!!\n";

  myfile.close();
{

I hope it'll at least help someone.

See ya!

T74marcell 03-11-2009 01:30 PM

I would actually put the standard headers before your own headers, like:
Quote:

#include <iostream>
#include <fstream>
#include "x.h"
In such a case even your original code should work without having any include lines in x.c (would have been included already in example.c).

Arch Linux

keen4linux 03-22-2010 09:10 AM

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.

johnsfine 03-22-2010 10:05 AM

Quote:

Originally Posted by T74marcell (Post 3472163)
I would actually put the standard headers before your own headers, like:

Code:

#include <iostream>
#include <fstream>
#include "x.h"


I think that is setting a maintenance land mine.

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.