简体   繁体   English

如何从C ++中的另一个头文件调用函数?

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

I have the following 3 files (1 *.cpp and 2 *.hpp) : 我有以下3个文件(1 * .cpp和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标头:

// first_func.hpp

...

double  first_func(double x, y, x)
{

    return x + y + x;
}

- the sec_func.hpp header: - sec_func.hpp标头:

// 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? 如何在sec_func.hpp文件中正确调用first_func?

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. 这就是为什么你的函数定义应该存在于编译单元(如.cpp)中,而不是在头文件中。 Headers should contain only the declarations (ie 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 inline函数
  • 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. 关键是每个想要实例化模板的编译单元(对它应用参数)都需要有完整的定义,这就是模板函数定义也进入标题的原因。

It's a bad practice to place function definition to .hpp files. 将函数定义.hpp文件中是一种不好的做法。 You should place only function prototypes there. 你应该只放置函数原型。 Like this: 像这样:

first_func.hpp: first_func.hpp:

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

first_func.cpp: 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. 然后,无论您想要调用first_func ,只需在该cpp模块中包含相应的first_func.hpp ,然后编写调用。

Thus, every your module consists of hpp with all declarations, and cpp with definitions (that is, the bodies). 因此,每个模块都包含带有所有声明的hpp和带有定义的cpp (即主体)。 When you need to reference something from this module, you include its hpp and use the name (of constant, variable, function, whatever). 当你需要从这个模块引用某些东西时,你要包含它的hpp并使用名称(常量,变量,函数等)。

And then you must link everything together: 然后你必须把所有东西连在一起:

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

To define a function in a header, you must mark it inline to prevent multiple definitions. 要在标题中定义函数,必须将其标记为inline以防止多个定义。

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;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM