I have two header files named Secure.h and FileMgt.h. To encrypt the File while saving i use Secure.h in FileMgt.h and in FileMgt.h i have declared some structure which is needed by Secure.h.The FileMgt.h is again include in another file called ElecB.h. I have used header guards in all the Files. The Problem is FileMgt.h is First included in ElecB.h. Since this file is already included in ElecB.h. Now its not including again in Secure.h.
Please give me Solution for this.
Thanks in advance.
Declare the structure in a third header file (with include guards) and include it in both Secure.h and FileMgt.h
Related
I currently started programming in C++.
My question is, whether when Im working with Lists I have to include this as a header file etc.?
Im working with this reference:
https://www.cplusplus.com/reference/list/list/
How can I know when I have to include something as a header file in general?
Thanks in advance.
In general, yes; otherwise, your code won't compile.
But when writing C++ there is always these considerations:
Do you put the include in your header or the cpp file? If your class or function header file doesn't require the include, then put it in your cpp file. This reduces compile time dependencies.
Does your file include another header file that includes the list header? If for instance you have a cpp file that uses some header (e.g. ListUtils.hpp) that already includes the list header, then you may decided not to explicitly include the list header.
I have an age-old question.
I'm developing an infrastructure code in C++ which has a lot of headers to be included in each header file.
I ideally would like to have everything included in a header file and then just to include that header file. That of course creates infinite-loop problem where a header is included in a header that includes the same header.
Doing #ifndef or #pragma once is also not going to help.
Is there any other clever way to achieve this?
Thanks
That of course creates infinite-loop problem where a header is included in a header that includes the same header.
Simply exclude the "superheader" from the set of headers that the "superheader" includes. That way you don't get the problem.
That said, doing this is likely going to cause translation units including the "superheader" to include headers unnecessarily which may adversely affect compilation times. As such, I don't recommend this approach.
My team and I are working on a pretty large project with many classes with their respective header and source files. We are trying to consolidate all includes from both C++ libraries and the projects class header files into one file called "Includes.h" which is included in every header file. One problem I have encountered when doing this is that the class header files are basically including themselves. I have included #pragma once at the top of every header file. When I comment out the #include "Controller.h" in the "#Includes.h" file, the errors for "Controller.h" go away.
Please Please Please and Pretty Please do not do this.
Prefer forward declarations. Then the individual include files.
Otherwise you change one include file and it has to compile the lot. I.e. waste of time.
Bascially get each header file to be able to compile with a blank cpp file. Minimum dependecies.
I've searched all over for some clarification on what
#pragma once
actually does and can't find definitive answers for some questions I still have.
Does
#pragma once
insure that the header file it is included in is only called once AS WELL AS that the headers which are included in said header file are not yet included? Also, if it is only called once, does that mean a .cpp file that needs a particular header will not be able to access it? If a header file is marked with
#pragma once
and included in a .cpp, can that header file be used again elsewhere?
These are the sorts of clarifications I am not finding. Sorry if there is documentation that clarifies this somewhere, but I really couldn't find any thing specific enough.
#pragma once only guards a single file in a single translation unit, not counting its sub-hierarchy of inclusion. (However, if the file's second inclusion is prevented, it doesn't have an opportunity to doubly-include anything else.)
You can still include it again from another .cpp.
The file is usually identified by its inode number.
Note that #pragma once is strictly nonstandard, and most still prefer traditional #ifndef header guards.
#pragma once causes the current source file to be included only once in a single compilation.
It's essentially similar to #include guards.
Does #pragma once insure that the header file it is included in is only called once AS WELL AS that the headers which are included in said header file are not yet included?
The pragma doesn't affect other headers. if the header with the pragma 'a.h' includes 'b.h', 'b.h' can be included again via a third header or directly.
Also, if it is only called once, does that mean a .cpp file that needs a particular header will not be able to access it?
You can include the header from anywhere you want, as many times as you see fit.
If a header file is marked with #pragma once and included in a .cpp, can that header file be used again elsewhere?
Yes, this is the normal practice with headers.
Where's the catch?
If you really need to include the headers more than once and every include performs a different operation than don't use pragma once or a sentry macro. These cases are not common.
A benefit to pragma once is that it saves you from bugs like having 2 header files that by chance have the same sentry macro. this can happen when 2 header files have the same file name and same coding style for macro names.
Is it even possible that relative path is correct, but that I nevertheless get the compilation error:
2>src\wfbuilderapp.cpp(15): fatal error C1083: Cannot open include file: '../project/include/public/core/paths.h': No such file or directory
In particular, is it possible that error occurs not because path is wrong, but because the included file "does not permit" that I have this binding between files ? The file where I am including and the included file are located in the same solution, in subfolders of folders located in different projects.
Here are my questions about #include:
Can I include any file as long as path is correct and included file is in the same solution ?
Can I include .cpp file ?
Do i sometimes need to include both .cpp and .h files ?
Can I include any file as long as path is correct and included file is in the same solution ?
If the path is correct, yes. It being in the same solution has no effect on the include. The compiler will look for the file in the places it's told to look (adding a file to a solution doesn't modify those places).
The proper way is adding the path to the Additional Include Directories in your Project properties.
Can I include .cpp file ?
Yes, but shouldn't, unless it's for bulk builds.
Do i sometimes need to include both .cpp and .h files ?
No. You can, but don't need to. You sometimes need to include headers, if the full definition of something defined in the header is needed. As for cpp files, see previous answer.
Can I include any file as long as path is correct
Yes. Note that I snipped the solution part.
Can I include .cpp file ?
Can, yes. Should? No.
Do I sometimes need to include both .cpp and .h files
If you "need" to do so, you are using the wrong tool for a problem. cpp files should never be #include'ed.
Can I include any file as long as path is correct and included file is
in the same solution ?
You can include any file you like, same solution or not. Whether the compiler can make any sense of it is a different question.
Can I include .cpp file ?
You can but you should not. cpp files should be 'included' in your project, not in other files.
Do i sometimes need to include both .cpp and .h files ?
See answer above. .h files only is the only sensible way to go