LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 03-11-2009, 12:07 PM   #1
Liah
LQ Newbie
 
Registered: Mar 2009
Posts: 4

Rep: Reputation: 0
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
 
Old 03-11-2009, 12:51 PM   #2
Liah
LQ Newbie
 
Registered: Mar 2009
Posts: 4

Original Poster
Rep: Reputation: 0
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!
 
Old 03-11-2009, 02:30 PM   #3
T74marcell
Member
 
Registered: Mar 2009
Posts: 102

Rep: Reputation: 18
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

Last edited by T74marcell; 03-14-2009 at 02:07 AM.
 
Old 03-22-2010, 10:10 AM   #4
keen4linux
LQ Newbie
 
Registered: Feb 2005
Location: Ahmedabad , India
Posts: 13

Rep: Reputation: 0
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.
 
Old 03-22-2010, 11:05 AM   #5
johnsfine
LQ Guru
 
Registered: Dec 2007
Distribution: Centos
Posts: 5,286

Rep: Reputation: 1190Reputation: 1190Reputation: 1190Reputation: 1190Reputation: 1190Reputation: 1190Reputation: 1190Reputation: 1190Reputation: 1190
Quote:
Originally Posted by T74marcell View Post
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.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Problems with ifstream/ofstream kornerr Programming 2 07-04-2005 12:31 PM
not able to get fd of ofstream using stdio_filebuf Devika Linux - General 0 03-28-2005 12:33 AM
ofstream and inheritence phoenix7 Programming 1 01-02-2005 03:52 AM
ofstream problem cgifalcon Linux - Software 2 10-19-2003 01:00 PM
c++ ofstream -> FILE * ChimpFace9000 Programming 1 08-25-2002 05:58 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 08:52 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration