C++ - Calling functions from other files and headers?
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
C++ - Calling functions from other files and headers?
I feel like there should be a cleaner way of doing this. I have one file, for example "a.cpp", calling a function from another file, "b.cpp". Currently I have it set up so that header for "b", "b.h", has the declaration of its functions. And then I'm just including "b.h" in "a.cpp". Do I have to include the "b" header file in "a" to be able to call a function from "b"? Or is there a better way I could be doing this? Like doing something different at compile? Thanks!
That's what you need to do, although you can put b.h in either a.cpp or a.h I would say that most people would choose to put it in the a.h
Actually, b.h should be included in the file where the functions are implemented; that is, b.cpp. If the "b" functions are being called from a.cpp, then the header file should be included there too.
If the "b" functions are being called from a.cpp, then the header file should be included there too.
It is not should, in that either will work. But because in practice most header files include more than just a function it may not be clearer to include it in the header.
When you have whole data structures in the header file it is often necessary for the header file to include others and then it becomes unnecessary to include it in the cpp file.
What I am saying is that if you have an a.h file as well as an a.cpp then you can include the b.h file in either. If for arguments sake a.h is a class that also includes the B class then the b.h must be included in that file or in an included file that also includes b.h - but it would be cleaner to include it rather than relying on another file to bring it in because that other file might change. Because the file a.cpp will include a.h it then doesn't need to include b.h keeping the implementation file a little cleaner.
This is just a matter of style and there are exceptions to this but in my experience I would say that people tend to put the include statements in the header files.
What I am saying is that if you have an a.h file as well as an a.cpp then you can include the b.h file in either. If for arguments sake a.h is a class that also includes the B class then the b.h must be included in that file or in an included file that also includes b.h - but it would be cleaner to include it rather than relying on another file to bring it in because that other file might change. Because the file a.cpp will include a.h it then doesn't need to include b.h keeping the implementation file a little cleaner.
Yes, I agree.
Quote:
Originally Posted by graemef
This is just a matter of style and there are exceptions to this but in my experience I would say that people tend to put the include statements in the header files.
I only include files where needed. So for example, if within the file A.cpp, I required to use std::cout for debug purposes, then I would include iostream within the .cpp file, not the A.h file. Furthermore, I always include project header files before system header files; the first project header file included in the .cpp file should be for the class being implemented (of course, this is not applicable to the .cpp file containing main()).
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.