header file dependancy

Started by
9 comments, last by AdamGL 17 years, 11 months ago
Hello. I am trying to make a logfile class that records certain messages to a logfile. I have other classes with functions that should use this. The problem is I don't want to make one header depend on another because one of them has a structure or class the other doesn't. Returning the message from a function won't work because if I want to return true or false for example, I wouldn't be able to. Please help.
bi-FreeSoftware
Advertisement
There's no need to have multiple references to the same header file. You can just include the header with the logfile class in the main source before the headers with classes that depend on it. #include statements are replaced during compile with the code contained in the files they reference, so read them as if the code were actually there.

As for using a function, I'm not entirely sure what you mean. Could you elaborate a bit?
Well, I have this:

Main.cpp
#include "logfile.cpp"#include "TextureHandler.cpp"int main()                  //I'm using the windows api but let use this as an ex.{    LOGFILE log;            //creates an instance of the LOGFILE class located in                             //lofile.cpp    TEXTUREHANDLER Texture; //creates an instance of the TEXTUREHANDLER class                                               //located in TextureHandler.cpp    Texture.AddTexture("C:\Hello\helloworld.tga"); //function that will return a                                                    //string    return 0;}


Logfile.cpp
class LOGFILE{   private:          ofstream Log;   public:          bool intitialize(char *);          bool RecordString(char *); };bool LOGFILE::RecordString(char *message){          Log << message;          return true;}

And finally, TextureHandler.cpp
class TEXTUREHANDLER{     public:           bool AddTexture(char *filename);};bool TEXTUREHANDLER::AddTexture(char *filename){        //code to add the texture                //now here is the problem! I want to write to the file, but that would mean I need to declare the instance globally so I can access it here, and I would need to declare it before this header. I don't want this. It makes this file dependant on the logfile.return true;}
bi-FreeSoftware
It's going to be dependant no matter what if you want to use it in there. Nature of the beast. My logger is not global, I just include it's header in each .cpp file that needs it (everyone of them). I don't put it in the header for the .cpp file though (you could but I don't).

"Those who would give up essential liberty to purchase a little temporary safety deserve neither liberty nor safety." --Benjamin Franklin

So that's the way to do it if I stick it in there. Is there a completely different way to do it, like not logging there, but maybe sending the text to some universal class wich then lets other functions access it?
bi-FreeSoftware
Use headers, don't just include CPP files.
Why? If I declare the class in the header file, I may as well write the functions in the header file too. So I just use a .cpp file. Is there another reason I should use the headers?
bi-FreeSoftware
You change the source in the source file, anything that includes that file will need recompiling as well (wasting time). Including a header file that has a prototype means that when you change the code in the source means you only have to recompile that one file without recompiling the files that use that function.

In a small project, the time difference is not noticable but get onto larger projects and its going to bite.

I believe there is another bigger reason not to though.

Steven Yau
[Blog] [Portfolio]

Quote:Original post by AdamGL
I may as well write the functions in the header file too. So I just use a .cpp file.


Why?!?!?

Source files are typically reserved for things that:
1) Contain a non-inline function definition (which will result in linker errors if included into multiple translation units, meaning #including .cpp files is a bad idea)
2) Need to be individually built (like the above, meaning the .cpp itself counts as one translation unit, meaning linker errors the moment you include said .cpp file in another)

You should be using a header file. Your build system won't try to needlessly compile it, and you'll be using the standard which everyone else uses. Namely, class definitions, and inline functions (which - if the function has it's full definition written out within the class itself, rather than just the forward decleration - will automatically be the case), and other bits that are needed in other places of the program that won't cause linker errors by #including.
Read this: Orgranizing Code Files in C and C++
John BoltonLocomotive Games (THQ)Current Project: Destroy All Humans (Wii). IN STORES NOW!

This topic is closed to new replies.

Advertisement