I am not a master of using c++.
I have a question about #include.
when I define var in the .h file, I use the class which needs to include, then I type to include in the .h file… if not do that, IDE will show red line or build show an error.
but the question is, cpp file also need to include the same file?
basically, I don’t have a problem with include, but I don’t know why…and I need to find out the basic rule of include…
i check the other people’s project in github, I found someone sometimes not include in .h, and instead type class key word for include. and then include in cpp, is it correct?
SomeFile.cpp should #include SomeFile.h at the top. otherwise you won’t be able to define the variables or functions that you prototyped in the header file.
I learned C++ a long time ago so I might get some facts here wrong and just wanted to give a heads up, but it shouldn’t be super far off.
Some .h (header) files don’t have the #include and I’ll try to explain why, but first I’ll try to explain why you need to include the .h (header) files.
You will need to include the .h (header) file if a file is trying to use some functions, variables or even declare a class that isn’t defined inside the file. For example, if you wanted to use “DrawDebugPoint” inside a character class, you would need the “#include ‘DrawDegubHelpers.h’” because the “DrawDebugPoint” isn’t defined or declared in the character class or it’s parent classes. Another example would be making a variable of type “ShooterCharacter.” You will also need the .h file of the “ShooterCharacter.”
Some .h (header) files do not have the #include. That is because when you use the #include you are copying all the functions and variables from that .h file into the file that has the #include. (This will happen when packaging your game) This will increase file size and may make the game slower because of all the unnecessary stuff.
So many developers forward declare the variable. For example they use “class ShooterCharater shooterCharacter;” instead of “ShooterCharacter shooterCharacter;” By adding the “class” in front of a variable you are forward declaring so you do not need to use the #include in the .h file, but you will need to in the .cpp file.
I think when u call the function in cpp file, like DrawDebugPoint, u need to include DrawDegubHelpers.h in cpp file, that makes sense.
But, in some situations. u use a class as var in .h file and also use the same class in .cpp file.
that means u need to include the class.h file in .h and .cpp? It feels stupid.
I think using the class keyword instead of including .h in .h, It feels smart…
But, I can’t find the unique answer, sometimes they include .h in .h, and sometimes use class keywords…
I can’t find out the rule.
Hi @MilionX, I think you misunderstood and I’m sorry if my expectations are horrible.
If you use the “class” keyword inside the class.h file you do not need the use the #include in the class.h file. However you will need the #include inside the class.cpp file.
Personally, I don’t think you should ever use the #include inside the class.h file because when packaging your game, the #include will copy the entire .h file into the file that has the include. For example if I use #include child.h inside the class.h, then during packaging, the computer will copy ALL of the functions and variables from the child.h and paste them into the class.h. This will increase file size and may slow down the game after packaging.
I also agree that this entire C++ thing in Unreal is kind of dumb. You run into a ton of issues you’ve never seen and spend a hell a time learn C++.
I know what u talk about.
I just curious the people who on github use include so “randomly”, so i have this question, but seems no matter how to use include, it’s hard to meet any problem…