473,320 Members | 2,071 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

ofstream argument in .h file

how to make this work

#ifndef HEAD_H
#define HEAD_H

void function (ofstream*);

#endif


how to make a header and it's implementation file containing fuctions that use pointers to ofstream class objects as arguments, i've tried including fstream in head.cpp and head.h and main.cpp in all combinations, thinking that that's where the problem lies, but got nothing. please help.
Feb 16 '08 #1
6 4907
Banfa
9,065 Expert Mod 8TB
Did you also include the statement

using namespace std;

in the cpp files you included the header into?
Feb 16 '08 #2
Did you also include the statement

using namespace std;

in the cpp files you included the header into?
here are my files.

header file called head.h:

Expand|Select|Wrap|Line Numbers
  1. #ifndef HEAD_H
  2. #define HEAD_H
  3.  
  4.  
  5. void myFunction (ofstream*);
  6.  
  7.  
  8. #endif
  9.  
implementation file head.cpp:

Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. #include <fstream>
  3. #include "head.h"
  4.  
  5. using namespace std;
  6.  
  7. void myFunction (ofstream* filePoint)
  8. {
  9.     if (!filePoint->is_open())
  10.         filePoint->open("file.txt");
  11.     filePoint <<"here, have some text."<<endl;
  12.     filePoint->close();
  13. }
  14.  
and main file main.cpp:

<snipped>

what am I doind wrong? i'll try to program a multifile project using two simple classes I'll write and try to figure this thing out... still, any advice is deeply appreciated. :)
Feb 17 '08 #3
Banfa
9,065 Expert Mod 8TB
I will start by asking you to remember to use code tags when posting code (I have added them), also we do not allow the posting of full solutions to homework questions, you should probably read our Posting Guidelines.

I have removed some of your code but left enough to illustrate the problem.

In your header file (head.h) you use an ofstrem, however at the top of your cpp files you have the code

Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. #include <fstream>
  3. #include "head.h"
  4.  
  5. using namespace std;
  6.  
The standard namespace is not visible until avter the 'using namespace std' line but you include head.h and try to use it before that line.

There are a number of solutions but here are 3
  1. In head.h change ofstream to std::ofstream, that is explicitly declare the namespace to be used.
  2. In the cpp file move the '#include "head.h"' line to below the 'using namespace standard' line so the name space is declared before you try to use it.
  3. In the file head.h include the headers require and declare any namespaces required at the top of that file (see note below)

Including Headers in Headers

There are basically 2 schools of thought on this the first is that headers should include the other headers they require so that when you include them you do not have to include other headers to make them work. The second is don't include headers in headers. There are advantages and disadvantages to both, for instance in the first method it is easy in a complex project to end up with a circular inclusion which with inclusion protection does cause the compiler a problem but normally causes a compile error where 2 class end up mutually dependent on each other. These can be tricky to work out. In the second a relatively minor change to a header that requires declarations to from a new header file can cause you to have to edit large numbers of source files in the project including that new header all over the place.
Feb 17 '08 #4
  1. In head.h change ofstream to std::ofstream, that is explicitly declare the namespace to be used.
  2. In the cpp file move the '#include "head.h"' line to below the 'using namespace standard' line so the name space is declared before you try to use it.
  3. In the file head.h include the headers require and declare any namespaces required at the top of that file (see note below)
THANK YOU SO MUCH. :) You have saved me hours of time. And just for the record, I'm a mechanical engineering student with absolutely no programming or IT courses in my curriculum, so nothing that i post here has anything to do with my homework, unless the forum opens topics on applied physics. :) Thank You again. :) Sorry about the missing code tags.
  1. I tried that, so i wrote:
    Expand|Select|Wrap|Line Numbers
    1. void myFunction (std::ofstream*);
    but the g++ compiler reported an error in main.cpp "ofstream was not declared in this scope"
  2. this worked fine
  3. this is something that bothers me, but worked fine

about the way 3 - if the problem lies in namespace usage, the best way is to write
Expand|Select|Wrap|Line Numbers
  1. using namespace std;
within the header and the corresponding implementation file, at least for me.

including many headers many times seems complicated, is it ever really necessary?
Feb 17 '08 #5
Banfa
9,065 Expert Mod 8TB
including many headers many times seems complicated, is it ever really necessary?
When you project has multiple modules, say 20, and each module consists of multiple code files and has 1 external header and at least 1 internal header then yes you can end up with quite complex include file issues.

If a small header change causes a change in half your code files then sometimes it can be viewed as better to have headers include headers.

However a precise answer to your question is that it is never really necessary it depends how you decide to set-up the project.
Feb 18 '08 #6
When you project has multiple modules, say 20, and each module consists of multiple code files and has 1 external header and at least 1 internal header then yes you can end up with quite complex include file issues.

If a small header change causes a change in half your code files then sometimes it can be viewed as better to have headers include headers.

However a precise answer to your question is that it is never really necessary it depends how you decide to set-up the project.
thank You, this is something that i can't just google. :)
Feb 18 '08 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: tornado | last post by:
hi all, This is quite puzzling me why is that ofstream constructor is unable to open a file for writing while, a ofstream "open" method can ! The c++ book by Herbert Schildt which i m readin...
4
by: Tom Johnson | last post by:
Hi all, I cant believe that Im stuck on such simple code, but I am so... Im trying to open a text file for writing but can never get the file to initially open. Heres what Im having trouble with....
2
by: slyphiad | last post by:
i'm kinda new at c++ so be patient ^_^ i was just wondering if u guys could help me to solve this problem that i had. i'm trying to create 5 sequential files using ofstream. this is what i...
5
by: Squid Seven | last post by:
I'm trying to use a pointer to an ofstream object and having problems: ofstream *sessionFile = NULL; if( directory == "" ) sessionFile = new ofstream( fileName.c_str(), ios::out ); else {
2
by: bballmitch | last post by:
I have the following code #include <cstdlib> #include <iostream> #include <fstream> int score_one=2345, score_two=1800, score_three=1500, score_four=1300, score_five=1100; int ...
5
by: Gary Wessle | last post by:
Hi I have a map<string, doublem_temperatures which gets updated often. I need to save the data to files corresponding to each string each time the map is updated, I am expecting about 80 files...
15
by: rEvolution27 | last post by:
I'm a c++ newbie here, trying out some stuff and when I try to compile this: void create() { char name; cout << "Creating a new timetable /n Please type a name for this timetable"; cin >name;...
5
by: Joe Hesse | last post by:
Hi, I have a C++ function that writes to an ofstream object. I would like to sometimes use it to write to cout. I realize that cout is of type ostream which is not ofstream. Since cout is "kind...
15
by: aaragon | last post by:
Hello, does anyone have a clue about this error? and how to solve it? It seems to be trivial to me, but not for the compiler. I'm using g++ 4.2 on an Ubuntu Linux system: // main() .......
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.