ofstream argument in .h file

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • everlast
    New Member
    • Nov 2007
    • 18

    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.
  • Banfa
    Recognized Expert Expert
    • Feb 2006
    • 9067

    #2
    Did you also include the statement

    using namespace std;

    in the cpp files you included the header into?

    Comment

    • everlast
      New Member
      • Nov 2007
      • 18

      #3
      Originally posted by Banfa
      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:

      [code=cpp]
      #ifndef HEAD_H
      #define HEAD_H


      void myFunction (ofstream*);


      #endif
      [/code]

      implementation file head.cpp:

      [code=cpp]
      #include <iostream>
      #include <fstream>
      #include "head.h"

      using namespace std;

      void myFunction (ofstream* filePoint)
      {
      if (!filePoint->is_open())
      filePoint->open("file.txt ");
      filePoint <<"here, have some text."<<endl;
      filePoint->close();
      }
      [/code]

      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. :)
      Last edited by Banfa; Feb 17 '08, 01:13 PM. Reason: Added Code tags removed full solution

      Comment

      • Banfa
        Recognized Expert Expert
        • Feb 2006
        • 9067

        #4
        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

        [code=cpp]
        #include <iostream>
        #include <fstream>
        #include "head.h"

        using namespace std;
        [/code]

        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.

        Comment

        • everlast
          New Member
          • Nov 2007
          • 18

          #5
          Originally posted by Banfa
          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:
            Code:
            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
          Code:
          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?

          Comment

          • Banfa
            Recognized Expert Expert
            • Feb 2006
            • 9067

            #6
            Originally posted by everlast
            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.

            Comment

            • everlast
              New Member
              • Nov 2007
              • 18

              #7
              Originally posted by Banfa
              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. :)

              Comment

              Working...