了解头文件

发布于 2024-11-01 16:10:53 字数 298 浏览 0 评论 0原文

我现在对头文件和函数有点困惑。

有人可以告诉我一个场景,我可以使用 4 个头文件进行 4 种不同的函数计算吗? (可以是简单的;我上学期才开始学习 C++。)教授让我很困惑,所以我希望在这里得到一些帮助。

我知道您在头文件中定义了一个函数。但我不明白如何在 main() 函数中使用头文件。

如果有人举个例子,我相信我能理解。他向我展示了 int add(int x, int y) 的加法,但我想了解除简单加法之外的函数。如何将它们放入头文件中,然后在 main() 中使用它们。

I'm sort of confused between header files and functions now.

Can someone tell me of a scenario where I could use 4 header files with 4 different calculations for functions? (It can be simple ones; I've only started C++ last semester.) And the professor has confused me so I was hoping for some assistance here.

I understand you define a function within the header file. But I don't understand how to use a header file within the main() function.

If someone showed an example I'm sure I could understand it. He's shown me one with addition with the int add(int x, int y) but I want to know functions other than just simple addition. And how can I put them within the header file and then use them in main().

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

踏月而来 2024-11-08 16:10:53

您可以通过在文件中键入 #include "foo.h" 来使用头文件。通常,函数声明放在头文件中,而将定义放在 cpp 文件中。

例如:

min.h

#ifndef MIN_H
#define MIN_H

int min( int a, int b );

#endif

min.cpp

#include "min.h"

int min( int a , int b ) {  
    // return a < b ? a : b; 
    // The following does the same as the commented out line
    if ( a < b )
        return a;
    else
        return b;
}

main.cpp

#include <stdio.h>
#include "min.h"

int main( void ) {  
    printf("min( 1 , 2) == %d\n", min(1,2));

    return 0;
}

在 Linux 上,您可以使用类似以下内容进行编译:

g++ main.cpp min.cpp -o minTest

这将为您提供一个名为 minTest 的可执行文件,然后您可以通过键入 ./minTest 来执行它

You use a header file by typing #include "foo.h" in your file. Typically declarations of functions go in header files while you put the definitions in cpp files.

For example:

min.h

#ifndef MIN_H
#define MIN_H

int min( int a, int b );

#endif

min.cpp

#include "min.h"

int min( int a , int b ) {  
    // return a < b ? a : b; 
    // The following does the same as the commented out line
    if ( a < b )
        return a;
    else
        return b;
}

main.cpp

#include <stdio.h>
#include "min.h"

int main( void ) {  
    printf("min( 1 , 2) == %d\n", min(1,2));

    return 0;
}

On Linux you would then compile using something like the following:

g++ main.cpp min.cpp -o minTest

which would give you an executable named minTest which you could then execute by typing ./minTest

蔚蓝源自深海 2024-11-08 16:10:53

在C++中,您需要了解函数声明和函数定义之间的区别。

  • 声明说“存在一个名为somename的函数,这是它的接口”。

    extern int somename(int x, int y);
    
  • 定义说“存在一个名为somename的函数,这就是它的实现方式”。

    int somename(int x, int y)
    {
        返回 x + y + 1;
    }
    

标头通常用于指定一个或多个类提供的接口,或者一个或多个函数提供的接口。它可能提供代码用户需要了解的其他信息。标头旨在供多个源文件使用;拥有一个供单个源文件使用的标头并没有多大意义(尽管在某些情况下这是有意义的)。

因此,通常,标头提供函数声明。

使事情变得复杂的是,可以内联定义函数。当函数被内联定义时,您可以将其实现放入文件(通常是头文件)中,编译器可能会优化函数的使用,以避免函数调用的开销。因此,您可能会创建一个包含以下内容的标头:

inline somename(int x, int y) { return x + y + 1; }

如果没有 inline 关键字,这将导致违反 ODR - 单一定义规则。 ODR 规定函数或全局可见对象只能在程序中定义一次。如果 header 中省略了关键字 inline,但 header 被用在多个文件中,那么每个文件都会定义 somename() 并且文件不能全部链接在一起,因为 somename() 会有多个定义。

In C++, you need to understand the difference between function declarations and function definitions.

  • A declaration says "a function called somename exists and this is its interface".

    extern int somename(int x, int y);
    
  • A definition says "a function called somename exists and this is how it is implemented".

    int somename(int x, int y)
    {
        return x + y + 1;
    }
    

A header is typically used to specify the interface provided by one or more classes, or the interface provided by one or more functions. It may provide other information that users of the code need to know. A header is intended for use by more than one source file; there isn't a lot of point in having a header that is intended for use by a single source file (though there are some cases where it makes sense).

So, normally, a header provides declarations of functions.

To complicate matters, functions can be defined inline. When a function is defined inline, then you place its implementation into a file (often a header file) and the compiler might optimize the use of the function to avoid the overhead of a function call. So you might create a header containing:

inline somename(int x, int y) { return x + y + 1; }

Without the inline keyword, this would lead to a violation of the ODR - One Definition Rule. The ODR says that a function or globally visible object may only be defined once in the program. If the keyword inline was omitted in the header, but the header was used in more than file, then each file would define somename() and the files could not all be linked together because there would be multiple definitions of somename().

暮色兮凉城 2024-11-08 16:10:53

当您想要在源文件 (.cpp) 中使用与实现该类的类不同的类时,头文件很有用。

要编译(创建 .o 对象文件),c++ 编译器至少需要查看您尝试使用的函数的原型;这些函数需要以某种方式实例化,就像您通常使用的任何函数一样。编译器想要验证参数的类型和返回值的类型是否与代码中使用函数的方式一致。

因此,您可以将

int add(int x, int y);

名为“addition.h”的文件和您的定义放入

#include "addition.h"
int add(int x, int y)
{
    return x+y;
}

名为“addition.cpp”的文件中。

在你的主文件中,你只需要..

#include "addition.h"

并且你可以随意使用addition(int,int),只要你不要忘记用你的main.cpp编译addition.cpp,在一个类似于这样的方式:
(本示例使用 G++)

$ g++ -c addition.cpp;
$ g++ -c main.cpp;
$ g++ main.o addition.o -o BinaryOutput.out

我希望这会有所帮助。

A header file is useful when you want to use a class in a source file (.cpp) that's different from the one the class was implemented in.

To compile (create the .o object files), c++ compilers require to at least see the prototype of the functions you are trying to use; the functions need to be instantiated in some way, like any function you would normally use. The compiler wants to verify that the types of the parameters and the type of your return value coincides with the way the function is used in your code.

So, you could put

int add(int x, int y);

in a file named "addition.h" and your definition,

#include "addition.h"
int add(int x, int y)
{
    return x+y;
}

in a file named "addition.cpp".

In your main file, you would just have to..

#include "addition.h"

and you could use addition(int,int) as much as you would like, as long as you don't forget to compile addition.cpp with your main.cpp, in a manner similar to this:
(this example uses G++)

$ g++ -c addition.cpp;
$ g++ -c main.cpp;
$ g++ main.o addition.o -o BinaryOutput.out

I hope this helps.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文