🔒 Locked
header file dependancy
Advertisement
Original Post
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.
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?
As for using a function, I'm not entirely sure what you mean. Could you elaborate a bit?
Well, I have this:
Main.cpp
Logfile.cpp
And finally, TextureHandler.cpp
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;}
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.
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.
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++
Topic Locked
This topic has been locked by a moderator. New replies are not allowed.
Sign in to reply to this topic.
Advertisement