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?
It’s a bad practice to place function definition to
.hppfiles. You should place only function prototypes there. Like this:first_func.hpp:
first_func.cpp:
The same for second func.
And then, wherever you want to call your
first_func, you just include correspondingfirst_func.hppin thatcppmodule, and write the call.Thus, every your module consists of
hppwith all declarations, andcppwith definitions (that is, the bodies). When you need to reference something from this module, you include itshppand use the name (of constant, variable, function, whatever).And then you must link everything together: