如何从 C++ 中的另一个头文件调用函数?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11602742/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to call a function from another header file in C++?
提问by tagoma
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?
回答by Kos
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.
这就是为什么您的函数定义应该驻留在编译单元(如 .cpp)中,而不是在头文件中的原因。头应该只包含声明(即没有主体),足以让其他编译单元知道如何调用它们。
For completeness, the functions that generally need to be defined in headers (as an exception) are:
为了完整起见,通常需要在头文件中定义的函数(作为例外)是:
inline
functions- template functions** (classes too)
inline
职能- 模板函数**(类也是)
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.
*
头文件实际上可以预编译,但这是加速编译的解决方案,它不会改变它们的目的;不要对此感到困惑。**
如果您使用显式模板实例化,您可以将模板函数定义放在标头之外,但这是一种罕见的情况;关键是每个想要实例化模板(对其应用参数)的编译单元都需要有完整的定义,这就是模板函数定义也进入头文件的原因。
回答by spacediver
It's a bad practice to place function definition to .hpp
files. You should place only function prototypes there. Like this:
将函数定义放在.hpp
文件中是一种不好的做法。你应该只在那里放置函数原型。像这样:
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
,您只需first_func.hpp
在该cpp
模块中包含相应的内容,然后编写调用。
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).
因此,您的每个模块都由hpp
所有声明和cpp
定义(即主体)组成。当您需要从该模块中引用某些内容时,您可以包含它hpp
并使用名称(常量、变量、函数等)。
And then you must link everything together:
然后您必须将所有内容链接在一起:
gcc main.cpp first_func.cpp second_func.cpp -o program
回答by Luchian Grigore
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;
}