相关疑难解决方法(0)

80
推荐指数
7
解决办法
3万
查看次数

是否需要包含头文件?

添加.h:

int add(int x, int y); // function prototype for add.h -- don't forget the semicolon!
Run Code Online (Sandbox Code Playgroud)

为了在 main.cpp 中使用这个头文件,我们必须 #include 它(使用引号,而不是尖括号)。

主要.cpp:

#include <iostream>
#include "add.h" // Insert contents of add.h at this point.  Note use of double quotes here.

int main()
{
    std::cout << "The sum of 3 and 4 is " << add(3, 4) << '\n';
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

添加.cpp:

#include "add.h"

int add(int x, int y)
{
    return x + y;
}
Run Code Online (Sandbox Code Playgroud)

嗨,我想知道,为什么我们在文件 add.cpp 中有#include“add.h”?我认为没有必要。

c++

2
推荐指数
1
解决办法
457
查看次数

标签 统计

c ×1

c++ ×1

compiler-errors ×1

gcc ×1