tags
tags

Reputation: 4060

How to call a function from another header file in C++?

I have the following 3 files (1 *.cpp and 2 *.hpp) :

the main program file:

// test.cpp

#include<iostream>
#include"first_func.hpp"
#include"sec_func.hpp"

int main()
{
    double x;
    x = 2.3;
    std::cout << sec_func(x) << std::endl;
}

- the first_func.hpp header:

// first_func.hpp

...

double  first_func(double x, y, x)
{

    return x + y + x;
}

- the sec_func.hpp header:

// sec_func.hpp

...

double sec_func(double x)
{
        double a, b, c;
        a = 3.4;
        b = 3.3;
        c = 2.5;

        return first_func(a,b,c) + x;
}

How do I properly call first_func from within the sec_func.hpp file?

Upvotes: 4

Views: 29850

Answers (3)

spacediver
spacediver

Reputation: 1493

It's a bad practice to place function definition to .hpp files. You should place only function prototypes there. Like this:

first_func.hpp:

double  first_func(double x, double y, double x);

first_func.cpp:

double  first_func(double x, double y, double x)
{
    return x + y + x;
}

The same for second func.

And then, wherever you want to call your first_func, you just include corresponding first_func.hpp in that cpp module, and write the call.

Thus, every your module consists of hpp with all declarations, and cpp with definitions (that is, the bodies). When you need to reference something from this module, you include its hpp and use the name (of constant, variable, function, whatever).

And then you must link everything together:

gcc main.cpp first_func.cpp second_func.cpp -o program

Upvotes: 5

Kos
Kos

Reputation: 72319

For most functions, the implementation should reside in a compilation unit, that is a file that is going to be compiled by itself and compiled once.

Headers are not to be compiled by themselves*, instead they are included by multiple compilation units.

That's why your function definitions should reside in compilation units (like .cpp), not in headers. Headers should contain only the declarations (i.e. without the body), just enough so that other compilation units would know how to call them.


For completeness, the functions that generally need to be defined in headers (as an exception) are:

  • inline functions
  • template functions** (classes too)

Footnotes:

* headers can actually be pre-compiled, but that's a solution for speeding up compilation and it doesn't alter their purpose; don't get confused by that.
** you can put template function definitions outside of the headers if you use explicit template instantiation, but that's a rare case; the point is that every compilation unit that wants to instantiate a template (apply arguments to it) needs to have its complete definition, that's why template function definitions go into headers too.

Upvotes: 7

Luchian Grigore
Luchian Grigore

Reputation: 258648

To define a function in a header, you must mark it inline to prevent multiple definitions.

If you want to do this instead of separating the implementation to a separate file, you'll need to provide a prototype before calling the function (either by including the header (prefered) or declaring the function yourself).

// sec_func.hpp

#include "first_func.hpp"
//or
double  first_func(double x, y, x); //declaration

double sec_func(double x)
{
        double a, b, c;
        a = 3.4;
        b = 3.3;
        c = 2.5;

        return first_func(a,b,c) + x;
}

Upvotes: 3

Related Questions