LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 04-11-2011, 08:45 PM   #1
golmschenk
Member
 
Registered: Nov 2009
Posts: 144

Rep: Reputation: 15
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!
 
Old 04-12-2011, 12:19 AM   #2
graemef
Senior Member
 
Registered: Nov 2005
Location: Hanoi
Distribution: Fedora 13, Ubuntu 10.04
Posts: 2,379

Rep: Reputation: 148Reputation: 148
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
 
Old 04-12-2011, 12:28 AM   #3
golmschenk
Member
 
Registered: Nov 2009
Posts: 144

Original Poster
Rep: Reputation: 15
Alright. I had put it in the header file. For some reason that just hadn't seemed right to me before. Thanks for the clarification!
 
Old 04-12-2011, 05:50 AM   #4
dwhitney67
Senior Member
 
Registered: Jun 2006
Location: Maryland
Distribution: Kubuntu, Fedora, RHEL
Posts: 1,541

Rep: Reputation: 335Reputation: 335Reputation: 335Reputation: 335
Quote:
Originally Posted by graemef View Post
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.
 
Old 04-12-2011, 07:33 AM   #5
graemef
Senior Member
 
Registered: Nov 2005
Location: Hanoi
Distribution: Fedora 13, Ubuntu 10.04
Posts: 2,379

Rep: Reputation: 148Reputation: 148
Quote:
Originally Posted by dwhitney67 View Post
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.
 
Old 04-12-2011, 07:41 AM   #6
dwhitney67
Senior Member
 
Registered: Jun 2006
Location: Maryland
Distribution: Kubuntu, Fedora, RHEL
Posts: 1,541

Rep: Reputation: 335Reputation: 335Reputation: 335Reputation: 335
I'm not sure if you are agreeing with me, or taking a different stance.

If this is B.h:
Code:
#ifndef B_H
#define B_H

#include <map>

class B
{
public:
   B();

   void insertKV(int key, int value);

private:
   std::map<int, int> myMap;
};

#endif
Then for B.cpp:
Code:
// We include B.h here to ensure that B's API is correctly followed below...
#include "B.h"

B::B()
{
}

void
B::insertKV(int key, int value)
{
   ...
}
If A.cpp requires the use of the class B, and needs to call the method insertKV(), then it would be as follows:
Code:
// Again, B.h is included to ensure that B's API is followed correctly...
#include "B.h"

int main()
{
   B b;

   b.insertKV(10, 20);
}
 
Old 04-12-2011, 08:18 AM   #7
graemef
Senior Member
 
Registered: Nov 2005
Location: Hanoi
Distribution: Fedora 13, Ubuntu 10.04
Posts: 2,379

Rep: Reputation: 148Reputation: 148
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.
 
Old 04-12-2011, 08:33 AM   #8
dwhitney67
Senior Member
 
Registered: Jun 2006
Location: Maryland
Distribution: Kubuntu, Fedora, RHEL
Posts: 1,541

Rep: Reputation: 335Reputation: 335Reputation: 335Reputation: 335
Quote:
Originally Posted by graemef View Post
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 View Post
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()).
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
calling functions python UpLink2008 Programming 3 08-29-2008 12:57 AM
QT calling functions ... How? Four Programming 3 02-11-2007 10:32 AM
Calling one of metacity's functions kiruxa Linux - General 0 11-25-2006 06:06 PM
calling functions outside the body(in c or c++).. bastin_gh Programming 1 07-06-2005 02:09 AM
Calling functions in C++ oulevon Programming 3 01-28-2005 03:14 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 04:33 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration